summaryrefslogtreecommitdiffstats
path: root/2019/day6/orbits.pl
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:40:39 +0300
committerYigit Sever2021-12-13 10:40:39 +0300
commitbf16b19b1f6deffd1983efca059db576f3b60ee5 (patch)
tree1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day6/orbits.pl
parent74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff)
downloadaoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip
2019, tracking
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}