Files
aoc-2024/day-02/main.raku
2024-12-03 10:34:33 +00:00

25 lines
566 B
Raku

use lib $*PROGRAM.parent.parent;
use Lib;
sub safe($report) {
my $diff = $report.rotor(2=>-1).map({ $_[1] - $_[0] }).List;
so (1 <= $diff.all <= 3 or -3 <= $diff.all <= -1)
}
grammar Solution does AOC {
token TOP { (^^ <report> \n)+ { make $/[0]>><report>>>.made; } }
token report { (<num> \h*)+ { make $/[0]>><num>>>.made;} }
token num { \d+ { make val($/.Str) } }
method part1(::?CLASS:U: $v) {
$v.map(&safe).Bag{True};
}
method part2(::?CLASS:U: $v) {
$v.map({so safe(.combinations(.elems - 1).any)}).Bag{True}
}
}
Solution.main()