summaryrefslogtreecommitdiffstats
path: root/2019/day6/orbits.pl
diff options
context:
space:
mode:
Diffstat (limited to '2019/day6/orbits.pl')
-rw-r--r--2019/day6/orbits.pl39
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 @@
1use strict;
2use warnings;
3
4my $file_name = $ARGV[0];
5
6if (not defined $file_name) {
7 die "missing filename\n";
8}
9
10open my $fh, "<", $file_name or die "Can't open $file_name, $!\n";
11
12my %orbit;
13
14while (<$fh>) {
15 chomp;
16 my ($from, $to) = split /\)/;
17 push @{ $orbit{$from} }, $to;
18}
19
20close $fh;
21
22my @to_value = ('---', 'COM');
23my $dist = 0;
24my $total = 0;
25
26while ( 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}