42 lines
815 B
Perl
42 lines
815 B
Perl
package Part1;
|
|
|
|
use 5.36.0;
|
|
use experimental 'declared_refs';
|
|
|
|
no warnings qw/ uninitialized /;
|
|
|
|
use List::AllUtils qw/ sum all /;
|
|
|
|
sub read_input ($file) {
|
|
my( $ordering, $manuals ) = split "\n\n", $file->slurp;
|
|
|
|
$ordering = [ map {
|
|
[ split '\|' ]
|
|
} split "\n", $ordering ];
|
|
|
|
$manuals = [ split "\n", $manuals ];
|
|
|
|
return { ordering => $ordering, manuals => $manuals };
|
|
}
|
|
|
|
sub good_manual( $ordering, $manual ) {
|
|
return all {
|
|
my $m = $_;
|
|
my ($i,$j) = map { index $manual, $m->[$_] } 0..1;
|
|
return 1 unless all { $_ > -1 } $i, $j;
|
|
return $i <= $j;
|
|
} @$ordering;
|
|
}
|
|
|
|
sub solve ($input) {
|
|
return sum
|
|
map {
|
|
my @m = split ',';
|
|
$m[@m/2];
|
|
}
|
|
grep { good_manual($input->{ordering},$_) }
|
|
$input->{manuals}->@*;
|
|
}
|
|
|
|
1;
|