Nim
type
AOCSolution[T,U] = tuple[part1: T, part2: U]
Vec2 = tuple[x,y: int]
proc removePaper(rolls: var seq[string]): int =
var toRemove: seq[Vec2]
for y, line in rolls:
for x, c in line:
if c != '@': continue
var adjacent = 0
for (dx, dy) in [(-1,-1),(0,-1),(1,-1),
(-1, 0), (1, 0),
(-1, 1),(0, 1),(1, 1)]:
let pos: Vec2 = (x+dx, y+dy)
if pos.x < 0 or pos.x >= rolls[0].len or
pos.y < 0 or pos.y >= rolls.len: continue
if rolls[pos.y][pos.x] == '@': inc adjacent
if adjacent < 4:
inc result
toRemove.add (x, y)
for (x, y) in toRemove: rolls[y][x] = '.'
proc solve(input: string): AOCSolution[int, int] =
var rolls = input.splitLines()
result.part1 = rolls.removePaper()
result.part2 = result.part1
while (let cnt = rolls.removePaper(); result.part2 += cnt; cnt) > 0:
discard
Today was so easy, that I decided to solve it twice, just for fun. First is a 2D traversal (see above). And then I did a node graph solution in a few minutes (in repo below). Both run in ~27 ms.
It's a bit concerning, because a simple puzzle can only mean that tomorrow will be a nightmare. Good Luck everyone, we will need it.
Full solution is at Codeberg: solution.nim