Day 3 - Advent of Code 2026

Working solutions for the day 3 puzzles.

Part One

""" day_03_01.py """

# usage: python3 day_03_01.py < input

import sys
from collections import namedtuple


Point = namedtuple('Point', ['x', 'y'])

with sys.stdin as infile:
    raw = infile.read()

forest = {Point(x, y) for y, row in enumerate(raw.splitlines())
          for x, obj in enumerate(row) if obj == '#'}

width = max(forest, key=lambda p: p.x).x
height = max(forest, key=lambda p: p.y).y

total = 0

for size in range(1, min(width, height) + 1):
    for x, y in forest:
        corners = {Point(x, y), Point(x + size, y + size),
                   Point(x + size, y), Point(x, y + size)}
        if corners <= forest:
            total += 1

print(total)

Part Two

""" day_03_02.py """

# usage: python3 day_03_02.py < input

import sys
from collections import namedtuple


Point = namedtuple('Point', ['x', 'y'])

with sys.stdin as infile:
    raw = infile.read()

forest = {Point(x, y) for y, row in enumerate(raw.splitlines())
          for x, obj in enumerate(row) if obj == '#'}

width = max(forest, key=lambda p: p.x).x
height = max(forest, key=lambda p: p.y).y

total = 0

for size in range(1, min(width, height) + 1):
    ds = {(dx, size - dx) for dx in range(1, size + 1)}
    for x, y in forest:
        for dx, dy in ds:
            corners = {Point(x, y), Point(x + dx - dy, y + dy + dx),
                       Point(x + dx, y + dy), Point(x - dy, y + dx)}
            if corners <= forest:
                total += 1

print(total)