From 238b94912013e91b8d8c34110fd06b474af880bc Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 4 Dec 2025 08:28:57 +0100 Subject: [PATCH] Day 4 part 2: shorter solution --- 2025/08.pl | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/2025/08.pl b/2025/08.pl index 3e4d8bf..fe63bd6 100755 --- a/2025/08.pl +++ b/2025/08.pl @@ -3,7 +3,7 @@ use v5.42; my @map = map { chomp; [ split // ] } <>; -my $xmax = $#{ $map[0] }; +my $xmax = $map[0]->$#*; my $ymax = $#map; my $sum; @@ -14,24 +14,22 @@ while (1) { for my $x (0 .. $xmax) { next if $map[$y][$x] ne '@'; my $c = 0; - for my $dy (-1 .. 1) { - my $ny = $y + $dy; - next if $ny < 0 || $ny > $ymax; - for my $dx (-1 .. 1) { - next if $dy == 0 && $dx == 0; - my $nx = $x + $dx; - next if $nx < 0 || $nx > $xmax; - $c++ if $map[$ny][$nx] eq '@'; - } + for my $dir ([-1, -1], [-1, 0], [-1, 1], + [ 0, -1], [ 0, 1], + [ 1, -1], [ 1, 0], [ 1, 1]) { + my ($nx, $ny) = ($x + $dir->[0], $y + $dir->[1]); + next if $nx < 0 || $nx > $xmax + || $ny < 0 || $ny > $ymax; + $c++ if $map[$ny][$nx] eq '@'; } push @to_remove, [$x, $y] if $c < 4; } } - last if !@to_remove; for my $p (@to_remove) { $map[$p->[1]][$p->[0]] = '.'; $sum++; } + last if !@to_remove; } say $sum; -- 2.47.3