X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=32.pl;fp=32.pl;h=f033a41ef21f66a422ec160b9f27e363b7f3870a;hb=b90eabab4d95a25b79a24a8d6c84c10886f36369;hp=0000000000000000000000000000000000000000;hpb=eecc1b2e975ad2efc811a0cf640e580a031d53a9;p=aoc2020.git diff --git a/32.pl b/32.pl new file mode 100755 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";