diff options
| author | Yigit Sever | 2021-12-13 10:40:39 +0300 |
|---|---|---|
| committer | Yigit Sever | 2021-12-13 10:40:39 +0300 |
| commit | bf16b19b1f6deffd1983efca059db576f3b60ee5 (patch) | |
| tree | 1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day6/orbits.pl | |
| parent | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff) | |
| download | aoc-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.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 | } | ||
