]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 4 part 2: shorter solution
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 4 Dec 2025 07:28:57 +0000 (08:28 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 4 Dec 2025 07:28:57 +0000 (08:28 +0100)
2025/08.pl

index 3e4d8bf94a20a3d9799e8804b359b31d57ef66c9..fe63bd6c565a3a0b2a9be9a7749e0cda661a02f2 100755 (executable)
@@ -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;