grammar Game {
rule TOP { 'Game' <id> ':' <cube>+ %% ';' }
token id { \d+ }
rule cube { [<number> <color>]+ %% ',' }
token number { \d+ }
token color { 'red' | 'blue' | 'green' }
}
class GameActions {
method TOP($/) { make (id => $<id>.made, cubes => $<cube>.map(*.made).Array).Hash }
method id($/) { make $/.Int }
method cube($/) { make ($<color>.map(*.made) Z=> $<number>.map(*.made)).Hash }
method number($/) { make $/.Int }
method color($/) { make $/.Str }
}
my %bag01 = (:red(12), :green(13), :blue(14)).Bag;
my ($sum01, $sum02);
for $*IN.lines -> $line {
my $game = Game.parse($line, :actions(GameActions)).made;
my $possible-bag = [(|)] $game<cubes>.map(*.Bag);
$sum01 += $game<id> if $possible-bag (<=) %bag01;
$sum02 += [*] $possible-bag.values;
}
put 'part 1: ', $sum01;
put 'part 2: ', $sum02;