Quick actions

cmd+k|ctrl+k

Navigation

Languages

AoC 2023, day 2

Snippet info

Language

Raku

Visibility

public

Author

seaker

Created

2023-12-02T06:34:25.032449Z

Updated

2023-12-04T09:39:58.649748Z

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;
INFO