diff options
Diffstat (limited to '2019/day6/orbits.pl')
-rw-r--r-- | 2019/day6/orbits.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/2019/day6/orbits.pl b/2019/day6/orbits.pl new file mode 100644 index 0000000..9f4aed5 --- /dev/null +++ b/2019/day6/orbits.pl | |||
@@ -0,0 +1,39 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | |||
4 | my $file_name = $ARGV[0]; | ||
5 | |||
6 | if (not defined $file_name) { | ||
7 | die "missing filename\n"; | ||
8 | } | ||
9 | |||
10 | open my $fh, "<", $file_name or die "Can't open $file_name, $!\n"; | ||
11 | |||
12 | my %orbit; | ||
13 | |||
14 | while (<$fh>) { | ||
15 | chomp; | ||
16 | my ($from, $to) = split /\)/; | ||
17 | push @{ $orbit{$from} }, $to; | ||
18 | } | ||
19 | |||
20 | close $fh; | ||
21 | |||
22 | my @to_value = ('---', 'COM'); | ||
23 | my $dist = 0; | ||
24 | my $total = 0; | ||
25 | |||
26 | while ( 1 ) { | ||
27 | |||
28 | my $cur = pop @to_value; | ||
29 | |||
30 | print $total and exit unless @to_value; | ||
31 | |||
32 | if ($cur eq '---') { | ||
33 | unshift @to_value, '---'; | ||
34 | $dist++; | ||
35 | } else { | ||
36 | $total += $dist; | ||
37 | unshift @to_value, @{ $orbit{$cur} } if exists $orbit{$cur}; | ||
38 | } | ||
39 | } | ||