Day 4 - Advent of Code 2025
4 December 2025
Working solutions for the day 4 puzzles.
Part One
""" day_04_01.py """
# usage: python3 day_04_01.py < input
import sys
def access(item, layout):
""" true if item has less than four neighbours """
x, y = item
return sum(1 for dy in [-1, 0, 1] for dx in [-1, 0, 1]
if (x + dx, y + dy) in layout) < 5
with sys.stdin as infile:
plan = {(i, j) for j, line in enumerate(infile)
for i, symbol in enumerate(line.strip()) if symbol == '@'}
total = sum(1 for roll in plan if access(roll, plan))
print(total)Part Two
""" day_04_02.py """
# usage: python3 day_04_02.py < input
import sys
def access(item, layout):
""" true if item has less than four neighbours """
x, y = item
return sum(1 for dy in [-1, 0, 1] for dx in [-1, 0, 1]
if (x + dx, y + dy) in layout) < 5
def accessible(layout):
""" true if at least one item is accessible """
for item in layout:
if access(item, layout):
return True
return False
with sys.stdin as infile:
plan = {(i, j) for j, line in enumerate(infile)
for i, symbol in enumerate(line.strip()) if symbol == '@'}
total = len(plan)
while accessible(plan):
plan = {roll for roll in plan if not access(roll, plan)}
total = total - len(plan)
print(total)