From 4bfec4870405d2472f7d7bd82f42b20b920d5fe3 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 11 Dec 2021 06:53:17 +0100 Subject: [PATCH] 22.pl: cleaner version --- 22.pl | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/22.pl b/22.pl index 221c686..9915675 100755 --- a/22.pl +++ b/22.pl @@ -4,43 +4,36 @@ use v5.16; my @m = map { chomp; [ split // ] } <>; -my $sum; my $step = 0; while (++$step) { - my %f; - my @q; + my (%f, @q); for my $x (0 .. 9) { for my $y (0 .. 9) { - next if $f{$x,$y}; next if ++$m[$y][$x] < 10; $f{$x,$y} = 1; - push @q, [$x,$y]; - say "flashed:$x,$y"; + push @q, [$x, $y]; } } - while (@q) { - my $p = shift @q; - my ($x2, $y2) = @$p; + while (my $p = shift @q) { + my ($x, $y) = @$p; for my $dx (-1 .. 1) { for my $dy (-1 .. 1) { - next if $dx == 0 && $dy == 0; - next if $x2+$dx < 0 || $x2+$dx > 9; - next if $y2+$dy < 0 || $y2+$dy > 9; - next if $f{$x2+$dx,$y2+$dy}; - say $x2+$dx, ',', $y2+$dy, ' >',$m[$y2+$dy][$x2+$dx]; - next if ++$m[$y2+$dy][$x2+$dx] < 10; - $f{$x2+$dx,$y2+$dy} = 1; - push @q, [$x2+$dx, $y2+$dy]; - say "flashed ", $x2+$dx,',',$y2+$dy; + my ($x2, $y2) = ($x+$dx, $y+$dy); + next if $x2 < 0 || $x2 > 9; + next if $y2 < 0 || $y2 > 9; + next if $f{$x2,$y2}; + next if ++$m[$y2][$x2] < 10; + $f{$x2,$y2} = 1; + push @q, [$x2, $y2]; } } } for my $p (keys %f) { my ($x, $y) = split /$;/, $p; $m[$y][$x] = 0; } - say "Step $_ flashes ", scalar keys %f; + say "Step $step flashes ", scalar keys %f; say join("\n", map { join('', @$_) } @m); - say; + say ''; last if keys %f == 100; } -- 2.43.5