#!/usr/bin/env raku
use v6.d;
use Test;
#use Grammar::Debugger;
# https://docs.raku.org/language/grammars
grammar G_24_02_a {
token TOP { <report>+ }
token report { <level>+ \v? }
token level { <value> \h? }
token value { <digit>+ }
}
class C_24_02_a {
method TOP($/ --> UInt) {
make sum $<report>».made;
}
method report($/ --> Bool) {
my @level of UInt = $<level>».made;
my Bool $is-increasing = [<] @level;
my Bool $is-decreasing = [>] @level;
my Bool $is-steady = $is-increasing || $is-decreasing;
my @deltas of UInt = @level.rotor(2 => -1).map: -> ($a, $b) { abs($b - $a) };
my Bool $deltas-ok = so 0 < all( @deltas ) < 4;
make $is-steady && $deltas-ok
}
method level($/ --> UInt) {
make $<value>.UInt
}
}
my $input = chomp q:to/INPUT/;
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
INPUT
is G_24_02_a.parse($input, actions => C_24_02_a.new).made, 2, "Day 2, Task a, Test"