]> www.fi.muni.cz Git - aoc.git/blob - 2016/19.pl
Day 25: examining the input
[aoc.git] / 2016 / 19.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 (my $bot = shift @todo) {
23         my ($lo, $hi) = sort { $a <=> $b } @{ $bot_has[$bot] };
24         if ($lo == 17 && $hi == 61) {
25                 say "$bot";
26                 last;
27         }
28         my @r = @{ $rule[$bot] };
29         if ($r[0] eq 'output') {
30                 $outp[$r[1]] = $lo;
31         } else {
32                 push @{ $bot_has[$r[1]] }, $lo;
33                 push @todo, $r[1] if @{ $bot_has[$r[1]] } == 2;
34         }
35         
36         if ($r[2] eq 'output') {
37                 $outp[$r[3]] = $hi;
38         } else {
39                 push @{ $bot_has[$r[3]] }, $hi;
40                 push @todo, $r[3] if @{ $bot_has[$r[3]] } == 2;
41         }
42 }
43         
44