]> www.fi.muni.cz Git - aoc.git/blobdiff - 2016/20.pl
Year 2016, days 1-10: so far pretty interesting
[aoc.git] / 2016 / 20.pl
diff --git a/2016/20.pl b/2016/20.pl
new file mode 100755 (executable)
index 0000000..15483c7
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/perl -w
+
+use strict;
+use v5.30;
+
+my @bot_has;
+my @todo;
+my @outp;
+my @rule;
+while (<>) {
+       chomp;
+       if (/value (\d+) goes to bot (\d+)/) {
+               push @{ $bot_has[$2] }, $1;
+               push @todo, $2 if @{ $bot_has[$2] } == 2;
+       } elsif (/bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)/) {
+               $rule[$1] = [ $2, $3, $4, $5 ];
+       } else {
+               die $_;
+       }
+}
+
+while (defined(my $bot = shift @todo)) {
+       my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] };
+       say "evaluating bot $bot which has $lo and $hi";
+       if ($lo == 17 && $hi == 61) {
+               say "$bot";
+       }
+       my @r = @{ $rule[$bot] };
+       if ($r[0] eq 'output') {
+               $outp[$r[1]] = $lo;
+               say "output $r[1] => $lo";
+       } else {
+               push @{ $bot_has[$r[1]] }, $lo;
+               push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2;
+       }
+       
+       if ($r[2] eq 'output') {
+               $outp[$r[3]] = $hi;
+               say "output $r[3] => $hi";
+       } else {
+               push @{ $bot_has[$r[3]] }, $hi;
+               push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2;
+       }
+}
+       
+say $outp[0] * $outp[1] * $outp[2];
+