diff options
author | June McEnroe <june@causal.agency> | 2020-12-16 00:40:20 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-16 00:40:20 -0500 |
commit | 65582ea0e9d23b4ddacf3fd19a12c5a7869e0831 (patch) | |
tree | 21c16ecd61212f2d59fe79cd8eed20b5c0503182 /2020 | |
parent | Update day 14 part 2 with the clever way (diff) | |
download | aoc-65582ea0e9d23b4ddacf3fd19a12c5a7869e0831.tar.gz aoc-65582ea0e9d23b4ddacf3fd19a12c5a7869e0831.zip |
Solve day 16 part 1
Oh god I barely know what I'm doing in perl.
Diffstat (limited to '2020')
-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"; |