]> www.fi.muni.cz Git - aoc2020.git/commitdiff
Day 16 - ugly
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 16 Dec 2020 07:16:49 +0000 (08:16 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 16 Dec 2020 07:16:49 +0000 (08:16 +0100)
31.pl [new file with mode: 0755]
32.pl [new file with mode: 0755]

diff --git a/31.pl b/31.pl
new file mode 100755 (executable)
index 0000000..ad2fc0e
--- /dev/null
+++ b/31.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+local $/ = "\n\n";
+my @ranges;
+for (split /\n/, <>) {
+       my (@cls) = /\A(.*): (\d+)-(\d+) or (\d+)-(\d+)/;
+       print "$2:$3,$4:$5.\n";
+       push @ranges, [$2, $3], [$4, $5];
+}
+
+$_ = <>;
+my @your = /(\d+)/g;
+print "Your:", join("|", @your), "\n";
+
+my $sum = 0;
+# my @nearby;
+for (split /\n/, <>) {
+       next if !/\d/;
+       my @n = /(\d+)/g;
+       # print "nearby:", join("|", @n), "\n";
+       # push @nearby, \@n;
+       NUM:
+       for my $num (@n) {
+               for my $r (@ranges) {
+                       if ($num >= $r->[0] && $num <= $r->[1]) {
+                               next NUM;
+                       }
+               }
+               print "$num is invalid\n";
+               $sum += $num;
+       }
+}
+
+print "sum=$sum\n";
diff --git a/32.pl b/32.pl
new file mode 100755 (executable)
index 0000000..f033a41
--- /dev/null
+++ b/32.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+local $/ = "\n\n";
+my @classes;
+for (split /\n/, <>) {
+       my (@cls) = /\A(.*): (\d+)-(\d+) or (\d+)-(\d+)/;
+       print "$2:$3,$4:$5.\n";
+       push @classes, \@cls;
+}
+
+$_ = <>;
+my @your = /(\d+)/g;
+print "Your:", join("|", @your), "\n";
+
+my @nearby;
+TICKET:
+for (split /\n/, <>) {
+       next if !/\d/;
+       my @n = /(\d+)/g;
+       NUM:
+       for my $num (@n) {
+               for my $r (@classes) {
+                       if (($num >= $r->[1] && $num <= $r->[2])
+                               || $num >= $r->[3] && $num <= $r->[4]) {
+                               next NUM;
+                       }
+               }
+               next TICKET;
+       }
+       # print "nearby:", join("|", @n), "\n";
+       push @nearby, \@n;
+}
+
+my %valid_cols;
+for my $cls (@classes) {
+       my ($name, $f1, $t1, $f2, $t2) = @$cls;
+       # print "Class $name:\n";
+       COL:
+       for my $col (0 .. $#your) {
+               # print "col $col\n";
+               for my $ticket (@nearby) {
+                       if (($ticket->[$col] < $f1 || $ticket->[$col] > $t1)
+                               && $ticket->[$col] < $f2 || $ticket->[$col] > $t2) {
+                               next COL;
+                       }
+               }
+               $valid_cols{$name}->{$col} = 1;
+               print "$name can be $col\n";
+       }
+}
+
+my $mul = 1;
+LOOP:
+while (keys %valid_cols) {
+       for my $class (keys %valid_cols) {
+               if (keys %{ $valid_cols{$class} } == 1) {
+                       my ($col) = keys %{ $valid_cols{$class} };
+                       print "$class is $col\n";
+                       delete $valid_cols{$class};
+                       for my $cl1 (keys %valid_cols) {
+                               delete $valid_cols{$cl1}->{$col};
+                       }
+                       $mul *= $your[$col] if $class =~ /\Adeparture/;
+                       next LOOP;
+               }
+       }
+}
+
+print "Total: $mul\n";