From bf16b19b1f6deffd1983efca059db576f3b60ee5 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 13 Dec 2021 10:40:39 +0300 Subject: 2019, tracking --- 2019/day6/orbits.pl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2019/day6/orbits.pl (limited to '2019/day6/orbits.pl') 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 @@ +use strict; +use warnings; + +my $file_name = $ARGV[0]; + +if (not defined $file_name) { + die "missing filename\n"; +} + +open my $fh, "<", $file_name or die "Can't open $file_name, $!\n"; + +my %orbit; + +while (<$fh>) { + chomp; + my ($from, $to) = split /\)/; + push @{ $orbit{$from} }, $to; +} + +close $fh; + +my @to_value = ('---', 'COM'); +my $dist = 0; +my $total = 0; + +while ( 1 ) { + + my $cur = pop @to_value; + + print $total and exit unless @to_value; + + if ($cur eq '---') { + unshift @to_value, '---'; + $dist++; + } else { + $total += $dist; + unshift @to_value, @{ $orbit{$cur} } if exists $orbit{$cur}; + } +} -- cgit v1.2.3-70-g09d2