From 068cb784668eebe8dbaa8afa56a14f5b9468c9e5 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 4 Dec 2025 06:13:07 +0100 Subject: [PATCH] Day 4: map walking --- 2025/07.pl | 29 +++++++++++++++++++++++++++++ 2025/08.pl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100755 2025/07.pl create mode 100755 2025/08.pl diff --git a/2025/07.pl b/2025/07.pl new file mode 100755 index 0000000..05cd289 --- /dev/null +++ b/2025/07.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w + +use v5.42; + +my @map = map { chomp; [ split // ] } <>; +my $xmax = $#{ $map[0] }; +my $ymax = $#map; + +my $sum; + +for my $y (0 .. $ymax) { + 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 '@'; + } + } + $sum++ if $c < 4; + } +} + +say $sum; diff --git a/2025/08.pl b/2025/08.pl new file mode 100755 index 0000000..3e4d8bf --- /dev/null +++ b/2025/08.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl -w + +use v5.42; + +my @map = map { chomp; [ split // ] } <>; +my $xmax = $#{ $map[0] }; +my $ymax = $#map; + +my $sum; + +while (1) { + my @to_remove; + for my $y (0 .. $ymax) { + 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 '@'; + } + } + push @to_remove, [$x, $y] if $c < 4; + } + } + last if !@to_remove; + for my $p (@to_remove) { + $map[$p->[1]][$p->[0]] = '.'; + $sum++; + } +} + +say $sum; -- 2.47.3