From 65582ea0e9d23b4ddacf3fd19a12c5a7869e0831 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 16 Dec 2020 00:40:20 -0500 Subject: Solve day 16 part 1 Oh god I barely know what I'm doing in perl. --- 2020/day16.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2020/day16.pl 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"; -- cgit 1.4.1