Quick actions

cmd+k|ctrl+k

Navigation

Languages

Advent of Code 2021, Day 11 Solution

Snippet info

Language

Raku

Visibility

public

Author

seaker

Created

2021-12-11T08:49:23.56722Z

Updated

2021-12-11T09:04:59.03382Z

sub inc(Array:D $o, Int:D \row, Int:D \col) {
    $o[row;col]++ if 0 ≤ row ≤ 9 && 0 ≤ col ≤ 9 && $o[row;col] != 0;
}

sub flash(Array:D $o, UInt:D \row, UInt:D \col) {
    $o[row;col] = 0;
    ([-1,-1], [-1,0], [-1,1],
     [ 0,-1],         [ 0,1],
     [ 1,-1], [ 1,0], [ 1,1]
    ).map({ inc($o, row + .[0], col + .[1]) });
}

my $o .= push([.comb».UInt]) for $*IN.lines;

my ($round, $cnt) = (1, 0);
loop {
    (^10 X ^10).map({ ++$o[.[0];.[1]] });
    my $cnt1 = 0;

    loop {        
        my $cnt0 = 0;
        for ^10 X ^10 -> (\row, \col) { 
            if $o[row;col] > 9 {
                flash($o, row, col);
                ++$cnt0;
            }
        }
        $cnt1 += $cnt0;
        last unless $cnt0 > 0;
    }
    $cnt += $cnt1;
    put 'part 1: ', $cnt if $round == 100;

    last if $cnt1 == 100;
    ++$round;
}
put 'part 2: ', $round;
INFO