Advent 2026 Day 3
A classic of the Advent problems: a grid!
Part 1
You are given a 2D grid containing trees # and empty ground .. The
task is to find grid-aligned squares, where each corner is a
tree. The edges and content of the squares can be anything.
I find that most of the time, it’s easiest and most flexible to
represent gridded input as a map (or hash table) of (row . col) -> char. This way you still get constant-time lookup (like an array),
but easier handling of the boundaries. In Guile, for example,
hash-ref of a non-existent key just returns #f. It makes sense to
add a method to read a grid to a hash table, and a utility method to
return the dimensions of the grid.
(define-module (aoc-input)
#:use-module (ice-9 hash-table)
#:use-module (ice-9 rdelim)
#:export (read-lines read-grid))
;; read-lines as before
(define (read-grid port)
(let loop ((grid (make-hash-table))
(row 0))
(let ((line (read-line port)))
(if (eof-object? line)
grid
(begin
(for-each (lambda (c col)
(hash-set! grid (cons row col) c))
(string->list line)
(iota (string-length line)))
(loop grid (+ row 1)))))))
(define-module (aoc grid)
#:use-module (ice-9 hash-table)
#:export (grid-size))
(define (grid-size grid)
(hash-fold (lambda (pos _ acc)
(cons (max (car acc) (+ 1 (car pos)))
(max (cdr acc) (+ 1 (cdr pos)))))
'(0 . 0)
grid))
To actually count the number of squares, I didn’t come up with
anything particularly elegant. For every tree in the grid, we can
check each size of square that would fit to the right and downward
(going left and/or upward would double-count). Just sum up over this,
which ends up being O(n^3). It’s never satisfying to write something
worse than even quadratic, but it’s the simplest. Start with a method
that will determine whether a square exists in the grid, given its
top-left corner and the side length.
(define (has-square? grid corner side-len)
(let ((row (car corner))
(col (cdr corner)))
(and
(eqv? (hash-ref grid corner) #\#)
(eqv? (hash-ref grid (cons row (+ col side-len))) #\#)
(eqv? (hash-ref grid (cons (+ row side-len) col)) #\#)
(eqv? (hash-ref grid (cons (+ row side-len)
(+ col side-len))) #\#)
#t))) ;; explicit true, which isn't strictly necessary
Then we can work over every tree, computing its maximum square size. Iterate over side lengths, sum over trees.
(define (part1 input)
(let ((size (grid-size input)))
(hash-fold (lambda (corner c acc)
(if (char=? c #\#)
(+ acc
(count (lambda (side-len)
(has-square? input corner side-len))
(iota (min
(- (car size) (car corner) 1)
(- (cdr size) (cdr corner) 1))
1)))
acc))
0 input)))
Part 2
I had an inkling that this would be the chosen curveball, given the focus on “grid-aligned” in part 1. The second part requires you to count squares with any rotation. As an example, a rotated square could look like the following.
.#...
....#
.....
#....
...#.
The key for me was to determine what offsets the corners lie at for a
given rotation. The top-left corner remains fixed as before. Our
concept of side-len essentially becomes a dx term to the top-right
corner. To generalise, I introduced a dy offset as well. In the
example above, in (x,y) coordinates, the square has corner (1, 0)
and offsets (3, 1). The top-right corner is at (x+dx,y+dy) = (4, 1). By symmetry, the bottom-left corner is at (x-dy, y+dx) = (0, 3). We can see that the bottom-right corner is then the combination
of these two offsets: (x+(dx - dy), y+(dx + dy)).
(define (has-rotated-square? grid corner dx dy)
(let ((row (car corner))
(col (cdr corner)))
(and
(eqv? (hash-ref grid corner) #\#)
(eqv? (hash-ref grid (cons
(+ row dy)
(+ col dx))) #\#)
(eqv? (hash-ref grid (cons
(+ row dx dy)
(+ col dx (- dy)))) #\#)
(eqv? (hash-ref grid (cons
(+ row dx)
(- col dy))) #\#)
#t)))
This leads us to another simple, if unsatisfying algorithm. We extend
the side-len search from part 1 to a search over dx and dy. The
easiest optimisation I see here is just to refine the bounds: clearly
all the corners must lie within the grid. We search over dy from 0
until we push the bottom-left corner out of the left side of the grid,
or the bottom-right corner out of the bottom of the grid. Similarly,
dx is restricted from 1 until the top-right corner goes out the
right side, or the bottom-right also goes out the bottom.
(define (part2 input)
(let ((size (grid-size input)))
(hash-fold (lambda (corner c acc)
(if (char=? c #\#)
(+ acc
(apply + (map (lambda (dy)
(count (lambda (dx)
(has-rotated-square? input corner dx dy))
(iota (min
(- (cdr size) (cdr corner) 1)
(- (car size) (+ (car corner) dy) 1))
1)))
(iota (min (+ (cdr corner) 1)
(- (car size) (car corner)))))))
acc))
0 input)))
Improvements
I think algorithmically there’s not much to improve. But we can add
some vectorisation. Each row of the input can be represented as a
vector of booleans. With Guile’s infinite-precision arithmetic, this
gives a single integer per row. Consider determining whether a single
row contains the top two corners of a grid-aligned square. Using
bit-shifts and bitwise-and (also known as logand), we can shift
the row on top of itself and get a binary mask. Lets look at the row
#..#..#
and search for a square with side length 3.
row: #..##.#
row << 3: ##.#...
and: #..#...
So there are two candidate top sides of a square in this row. We can do the same for the bottom side of the square, then do a population count (i.e. count all the nonzeros bits) to get the number of squares of a given side length. Introduce a simple convenience function for summing a function over a list.
(define-module (aoc util)
#:use-module (srfi srfi-1)
#:export (sum-over))
(define (sum-over f xs)
(fold (lambda (x total) (+ total (f x))) 0 xs))
Now we can write the vectorised square-finding method. This is
extended with the (dx,dy) notion from part 2.
(use-modules ((srfi srfi-60)
#:select
((bit-count . popcount)
bitwise-and
list->integer)))
(define* (count-squares grid row dx #:key (dy 0))
(let ((first-row (vector-ref grid row))
(second-row (vector-ref grid (+ row dy)))
(third-row (vector-ref grid (+ row dx)))
(fourth-row (vector-ref grid (+ row dx dy))))
(popcount
(bitwise-and
first-row
(ash second-row dx)
(ash third-row (- dy))
(ash fourth-row (- dx dy))))))
Then the vectorised drivers for the first parts run over each row, and
each (dx, dy) pair (setting dy to 0 for part 1). To avoid
calculating the size separately, I just used the grid input that is
already sitting around.
(define input-vectorised
(list->vector
(map (lambda (s)
(list->integer (map (lambda (c) (char=? c #\#)) (string->list s))))
(call-with-input-file "../input/day3" read-lines))))
(define (part1-vectorised input input-vectorised)
(let* ((size (grid-size input))
(size (max (car size) (cdr size))))
(sum-over
(lambda (side-len)
(sum-over
(lambda (row)
(count-squares input-vectorised row side-len))
(iota (- size side-len))))
(iota (- size 1) 1))))
(define (part2-vectorised input input-vectorised)
(let* ((size (grid-size input))
(size (max (car size) (cdr size))))
(sum-over
(lambda (dx)
(sum-over
(lambda (dy)
(sum-over
(lambda (row)
(count-squares input-vectorised row dx #:dy dy))
(iota (- size (+ dx dy)))))
(iota (- size dx))))
(iota (- size 1) 1))))
Given the relatively sparse input, this actually only gives about a 2.5x speedup. The short-circuiting corner checks and checking squares only from trees probably help this. However, on the worst-case all-tree grid, I see about a 60x speedup (0.251s vs. 15.14s).