diff options
-rw-r--r-- | 2020/day16.pl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/2020/day16.pl b/2020/day16.pl new file mode 100644 index 0000000..8ebe521 --- /dev/null +++ b/2020/day16.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; + +my (%rules, @ticket, @nearby); +while (<>) { + chomp; + if (/([^:]+): (\d+)-(\d+) or (\d+)-(\d+)/) { + $rules{$1} = [$2, $3, $4, $5]; + } elsif (/your ticket:/) { + <>; + chomp; + @ticket = split ','; + } elsif (/^[\d,]+$/) { + my @fields = split ','; + push @nearby, \@fields; + } +} + +my $error = 0; +foreach my $t (@nearby) { + foreach my $f (@$t) { + my $valid = 0; + foreach my $r (values %rules) { + $valid = 1 if $f >= $$r[0] && $f <= $$r[1]; + $valid = 1 if $f >= $$r[2] && $f <= $$r[3]; + } + $error += $f unless $valid; + } +} +print "$error\n"; |