]> www.fi.muni.cz Git - aoc.git/blob - 2016/20.pl
Year 2016, days 1-10: so far pretty interesting
[aoc.git] / 2016 / 20.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use v5.30;
5
6 my @bot_has;
7 my @todo;
8 my @outp;
9 my @rule;
10 while (<>) {
11         chomp;
12         if (/value (\d+) goes to bot (\d+)/) {
13                 push @{ $bot_has[$2] }, $1;
14                 push @todo, $2 if @{ $bot_has[$2] } == 2;
15         } elsif (/bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)/) {
16                 $rule[$1] = [ $2, $3, $4, $5 ];
17         } else {
18                 die $_;
19         }
20 }
21
22 while (defined(my $bot = shift @todo)) {
23         my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] };
24         say "evaluating bot $bot which has $lo and $hi";
25         if ($lo == 17 && $hi == 61) {
26                 say "$bot";
27         }
28         my @r = @{ $rule[$bot] };
29         if ($r[0] eq 'output') {
30                 $outp[$r[1]] = $lo;
31                 say "output $r[1] => $lo";
32         } else {
33                 push @{ $bot_has[$r[1]] }, $lo;
34                 push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2;
35         }
36         
37         if ($r[2] eq 'output') {
38                 $outp[$r[3]] = $hi;
39                 say "output $r[3] => $hi";
40         } else {
41                 push @{ $bot_has[$r[3]] }, $hi;
42                 push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2;
43         }
44 }
45         
46 say $outp[0] * $outp[1] * $outp[2];
47