Josiah Winslow solves Advent of Code

Foggy Forest

Published: 2026-12-03 Original Prompt

Part 1

Another day, another grid puzzle. This time, we’re finding grid-aligned “squares” of trees in a large forest.

My solution framework includes a custom grids module for just this kind of puzzle, with several convenience functions for dealing with grids and coordinates in grids. Grids themselves are represented as dicts that map coordinates to tiles, and coordinates are represented as tuples of two ints (a row and column in the grid).

The grids functions I will be using today are parse_grid() for creating the grid, and add_points() and subtract_points() for doing math to grid coordinates. These functions are not very long or hard to understand;1 in fact, here they are in their entirety.

utils\grids.py
from collections.abc import Callable, Iterable
type GridPoint = tuple[int, int]
type Grid[Item] = dict[GridPoint, Item]
def add_points(a: GridPoint, b: GridPoint) -> GridPoint:
return a[0] + b[0], a[1] + b[1]
def subtract_points(a: GridPoint, b: GridPoint) -> GridPoint:
return a[0] - b[0], a[1] - b[1]
def parse_grid[Item](
raw_grid: list[str],
item_factory: Callable[[str], Item] = str,
*,
ignore_chars: Iterable[str] = "",
) -> Grid[Item]:
result: Grid[Item] = {}
ignore = set(ignore_chars)
for row, line in enumerate(raw_grid):
for col, char in enumerate(line):
if char in ignore:
continue
result[row, col] = item_factory(char)
return result

So because only the locations of the trees are important here, the first thing I did was call parse_grid() ignoring the ground tiles (.), and get a view on the keys of the grid — the coordinates of the tree tiles.

2026\day03\solution.py
class Solution(StrSplitSolution):
def part_1(self) -> int:
points = parse_grid(self.input, ignore_chars=".").keys()
...

The way we will find squares in the grid is by choosing two points p1 and p2 to be two of the corners of the square; the distance between them will be the side length of the square, so we can rotate 90 degrees and move that far along from those points to get the other two points p3 and p4.

........
.1--->2.
.|....|.
.|....|.
.|....|.
.v....v.
.3....4.
........

Of course, there is the concern that we could end up counting duplicate squares (or non-grid-aligned squares) this way, so we have to ensure that we choose a clear and consistent convention for selecting squares. To that end, let’s assume the following:

  1. The line from p1 to p2 must point straight to the right. This ensures that the square is aligned to the grid — and that each possible p1-to-p2 line we could choose is unique.
  2. The lines from p1 to p3 and from p2 to p4 must point straight down. This ensures that the positions of p1 and p2 uniquely determine the entire square shape — which, together with the first assumption, ensures that each possible square is unique.

The selection of all possible pairs of p1 and p2 points will be our first step. (I chose to use itertools.product() to get all pairs of points, rather than two nested for loops.) Once I have a pair of points, I use my subtract_points() function to get the difference between them, and I use continue to skip these points if their difference isn’t 0 in the rows and positive in the columns.

2026\day03\solution.py
from itertools import product
class Solution(StrSplitSolution):
def part_1(self) -> int:
...
# Each pair of points could form one side of a square
for p1, p2 in product(points, repeat=2):
dr, dc = subtract_points(p2, p1)
# To avoid counting duplicate squares, only consider sides
# extending straight to the right
if dr != 0 or dc <= 0:
continue
...

And now that our first assumption is satisfied, the selection of the corresponding p3 and p4 points will be our second step. They will extend straight down from p1 and p2, so I define a downwards offset of the right length and use my add_points() function to add it to p1 and p2. Then if p3 and p4 are both in the set of points,2 I know I’ve found a square, so I add 1 to a running count of squares.

2026\day03\solution.py
from itertools import product
class Solution(StrSplitSolution):
def part_1(self) -> int:
...
num_squares = 0
# Each pair of points could form one side of a square
for p1, p2 in product(points, repeat=2):
dr, dc = subtract_points(p2, p1)
# To avoid counting duplicate squares, only consider sides
# extending straight to the right
if dr != 0 or dc <= 0:
continue
# We can get the square's other corners by moving down from
# both initial corners
offset = (dc, 0)
p3, p4 = add_points(p1, offset), add_points(p2, offset)
if p3 in points and p4 in points:
num_squares += 1
return num_squares

The end result is a nice and neat solution that we arrived at “fair and square”.

Part 2

This time, the squares we’re looking for don’t have to be aligned to the grid. Believe it or not, we don’t have to go back to “square one”; not much will actually change about our square-finding process. Let’s factor out the first part into a function shared by both parts, with an extra grid_aligned parameter we can enable in one part but not the other.

2026\day03\solution.py
...
class Solution(StrSplitSolution):
def _solve(self, grid_aligned: bool) -> int:
... # Part 1 code from before
def part_1(self) -> int:
return self._solve(grid_aligned=True)
def part_2(self) -> int:
return self._solve(grid_aligned=False)

We can in fact follow the same general process from before: choose two initial points p1 and p2, then find p3 and p4 at an offset from those points. There will only be two main differences to account for the non-grid-alignedness of the squares:

These small changes can easily be applied to our previous code. As well, to handle Part 1, we still want the same pointing-right condition we had before if grid_aligned is true.

2026\day03\solution.py
...
class Solution(StrSplitSolution):
...
def _solve(self, grid_aligned: bool) -> int:
points = parse_grid(self.input, ignore_chars=".").keys()
num_squares = 0
# Each pair of points could form one side of a square
for p1, p2 in product(points, repeat=2):
dr, dc = subtract_points(p2, p1)
# To avoid counting duplicate squares, only consider sides
# extending to the lower-right (but not straight down)
if dr < 0 or dc <= 0:
continue
# Grid-aligned squares must have this side extend straight
# to the right
if grid_aligned and dr != 0:
continue
# We can get the square's other corners by moving clockwise
# from both initial corners
offset = (dc, -dr)
p3, p4 = add_points(p1, offset), add_points(p2, offset)
if p3 in points and p4 in points:
num_squares += 1
return num_squares

Looks like we were fairly easily able to get this puzzle “squared away”. Which is good, because I didn’t think it would be this easy going into it.

Footnotes

  1. The only thing about these functions that I don’t think is self-explanatory is the item_factory parameter of parse_grid(). That’s for the occasional puzzle where each tile should be turned into something other than a single character; for example, parse_grid(self.input, int) will pass each tile character to the int function, making each tile an int. ↩

  2. We don’t have to check p1 and p2 because we chose them from the set of points in the first place, so we already know they’re in the set. ↩

  3. Rotating (dr, dc) 90 degrees counterclockwise, on the other hand, leads to new coordinates of (-dc, dr). It’s tricky to remember which formula goes to which direction, but I find it usually becomes clear after drawing out some example points. ↩