Skip to content

Instantly share code, notes, and snippets.

@mscha
Last active December 4, 2025 11:11
Show Gist options
  • Select an option

  • Save mscha/17e1c82024694573710c16a693ded597 to your computer and use it in GitHub Desktop.

Select an option

Save mscha/17e1c82024694573710c16a693ded597 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 4
#!/usr/bin/env raku
use v6.d;
$*OUT.out-buffer = False; # Autoflush
# Advent of Code 2025 day 4 -- https://adventofcode.com/2025/day/4
class PaperGrid
{
has Str $.map;
has @.grid = $!map.lines».comb».Array;
has Int $.height = +@!grid;
has Int $.width = +@!grid[0];
method has-paper(Int $x, Int $y)
{
return False unless 0 ≤ $x < $!width;
return False unless 0 ≤ $y < $!height;
return @!grid[$y;$x] eq '@';
}
method clear(Int $x, Int $y) { @!grid[$y;$x] = '.' }
method occupied-neighbours(Int $x, Int $y)
{
return ($x-1 .. $x+1 X $y-1 .. $y+1).grep(-> ($x1, $y1) {
($x1 ≠ $x || $y1 ≠ $y) && self.has-paper($x1, $y1)
});
}
method has-accessible-paper(Int $x, Int $y)
{
self.has-paper($x, $y) && self.occupied-neighbours($x, $y) < 4;
}
method accessible-paper()
{
(^$!width X ^$!height).grep(-> ($x, $y) {
self.has-accessible-paper($x, $y)
});
}
method remove-paper(Bool :$verbose = False)
{
my $count = 0;
while my @loc = self.accessible-paper {
self.clear(|$_) for @loc;
$count += @loc;
say "{+@loc} removed, $count total" if $verbose;
}
return $count;
}
method Str { @!grid».join.join("\n") }
method gist { self.Str }
}
sub MAIN(IO() $inputfile where *.f = 'aoc04.input', Bool :v(:$verbose) = False)
{
my $grid = PaperGrid.new(:map($inputfile.slurp));
say $grid if $verbose;
say "Part one: ", +$grid.accessible-paper;
say "Part two: ", $grid.remove-paper(:$verbose);
say $grid if $verbose;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment