aboutsummaryrefslogtreecommitdiff
path: root/day06.pl
blob: 218531b03be8300328092df81ed78a966792d452 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/perl -w
use v5.16;

use File::Slurper 'read_lines';

my %orbits;

for (read_lines("day06")) {
    my ($in, $out) = split /\)/;
    $orbits{$out} = $in;
}

my $p1 = 0;
for (keys %orbits) {
    while ($_ ne "COM") {
        $p1++;
        $_ = $orbits{$_};
    }
}
say $p1;  # 147223

my (@path1, @path2);
$_ = "YOU"; push @path1, $_ = $orbits{$_} while ($_ ne "COM");
$_ = "SAN"; push @path2, $_ = $orbits{$_} while ($_ ne "COM");

while ($path1[-1] eq $path2[-1]) {
    pop @path1;
    pop @path2;
}

say @path1 + @path2;  # 340