Advent of Code 2026 Day 2
Table of Contents
1. Advent of Code 2026 Day 3
At this point, I'm slipping behind. I haven't had the time each day to sit down and do the AoC problem, so I will continue writing these articles as I finish the respective problems.
1.1. Part 1
I thought this puzzle was really fun! To see the full description, you can visit the puzzle page, but in a nutshell, given a set of coordinates in a grid, find how many form a square.
I made a solution for this pretty quickly, it essentially finds the distance between every point of squares on a horizontal line and checks below them to see if a square is formed.
First, load the data:
1: (require '[clojure.string :as str])
2: (def lines (clojure.string/split-lines (slurp "input.txt")))
3: (def grid
4: (mapv vec lines))
The result being:
1: [[\# \. \. \. \#]
2: [\. \. \. \. \.]
3: [\. \. \# \. \#]
4: [\. \. \. \. \.]
5: [\# \. \# \. \#]]
Then, I find the potential corner points in a row and partition them out:
1: (defn corners-in-row [row]
2: (partition-corners (keep-indexed #(when (= %2 \#) %1) row)))
3:
4: (defn partition-corners [row]
5: (mapcat (fn [[i x]]
6: (map (fn [y] (list x y))
7: (drop (inc i) row)))
8: (map-indexed vector row)))
This just gets the indexes of # characters in a row and partitions them out into pairs such that there aren't any duplicates in either direction, so \((0, 1)\) and \((1, 0)\) would be considered the same.
In this case, the result for something like the last row would be \(\{(0, 2), (0, 4), (2, 4)\}\) so you can see how convenient that is. Note that these pairs aren't \((x, y)\) coordinates, they are \((x_1, x_2)\) coordinates on the same row.
The square? function I implemented is very simple; it calculates \(\Delta x\) and looks below the given points to see if they are valid. If they both are, we have a square!
1: (defn square? [row pair m]
2: (let [tl (first pair) ; top-right corner
3: tr (second pair) ; top-left corner
4: len (- tr tl)]
5: (and
6: (hash? (+ row len) tl m)
7: (hash? (+ row len) tr m))))
Finally, I just put it all together and sum it up:
1: (defn squares-in-row [row grid]
2: (count
3: (filter true?
4: (map #(square? row % grid)
5: (corners-in-row (nth grid row))))))
6:
7: (def sol-1
8: (reduce +
9: (map #(squares-in-row % grid)
10: (range (count grid)))))
1.2. Part two
Part two took me for a tumble. I should have seen it coming, but alas, hindsight is 20/20. For this part, the squares can be rotated, not just grid-aligned. Obviously my existing solution isn't going to work very well for this, it makes too many assumptions.
I had a few ideas, but ultimately the idea that I ended up going with was super simple. I basically extended that corners-in-row function to work across the entire grid and made a new rotsquare? function to use that data to determine if a square exists given two points. The formula I used to calculate the four points is super simple stuff.
- We need four points \((x_1, y_1), (x_2, y_2), (x_3, y_3)\), and \((x_4, y_4)\). We already have the first two.
- To calculate the other two, I determine \(\Delta x = x_2 - x_1\) and \(\Delta y = y_2 - y_1\).
- With that, \((x_3 , y_3) = (x_1 - \Delta y, y_1 + \Delta x)\) and \((x_4 , y_4) = (x_2 - \Delta y, y_2 + \Delta x)\)
That's really it. I avoided doing this at first because I wanted a solution that was more elegant like transposing a matrix over a line between two points but that's not any better honestly.
Here's the (somewhat ugly) code for rotsquare?:
1: (defn rotsquare? [pair1 pair2 m]
2: (let [[x1 y1] pair1
3: [x2 y2] pair2
4: dx (- x2 x1)
5: dy (- y2 y1)
6: x3 (- x1 dy)
7: y3 (+ y1 dx)
8: x4 (- x2 dy)
9: y4 (+ y2 dx)]
10: (when (and
11: (hash? y3 x3 m)
12: (hash? y4 x4 m))
13: [pair1 pair2 [x3 y3] [x4 y4]])))
Finally, put it all together:
1: (def detected-squares (filter some?
2: (map #(rotsquare? (first %) (second %) grid)
3: (partition-corners (all-hashes grid)))))
4:
5: (def sol-2
6: (count (set (map set detected-squares))))
And we're done!
1.3. Final code
Day three's problem was really good! I liked having to take out a piece of paper and pencil down some math to make sure everything lined up how I thought it would.
Click to reveal solution
1: (require '[clojure.string :as str])
2: (require '[clojure.math :as math])
3:
4: (def lines (str/split-lines (slurp "test-input.txt")))
5: (def grid
6: (mapv vec lines))
7:
8: (defn partition-corners [row]
9: (mapcat (fn [[i x]]
10: (map (fn [y] (list x y))
11: (drop (inc i) row)))
12: (map-indexed vector row)))
13:
14: (defn corners-in-row [row]
15: (partition-corners (keep-indexed #(when (= %2 \#) %1) row)))
16:
17: (defn hash? [row col map]
18: (= \# (get-in map [row col])))
19:
20: (defn square? [row pair m]
21: (let [tl (first pair)
22: tr (second pair)
23: len (- tr tl)]
24: (and
25: (hash? (+ row len) tl m)
26: (hash? (+ row len) tr m))))
27:
28:
29: (defn squares-in-row [row grid]
30: (count
31: (filter true?
32: (map #(square? row % grid)
33: (corners-in-row (nth grid row))))))
34:
35: (def sol-1
36: (reduce +
37: (map #(squares-in-row % grid)
38: (range (count grid)))))
39:
40: (defn all-hashes [grid]
41: (apply concat (filter seq
42: (map-indexed
43: (fn [row itm]
44: (filter some?
45: (map-indexed
46: (fn [col point]
47: (when (hash? row col grid) [col row]))
48: itm)))
49: grid))))
50:
51: (defn rotsquare? [pair1 pair2 m]
52: (let [[x1 y1] pair1
53: [x2 y2] pair2
54: dx (- x2 x1)
55: dy (- y2 y1)
56: x3 (- x1 dy)
57: y3 (+ y1 dx)
58: x4 (- x2 dy)
59: y4 (+ y2 dx)]
60: (when (and
61: (hash? y3 x3 m)
62: (hash? y4 x4 m))
63: [pair1 pair2 [x3 y3] [x4 y4]])))
64:
65: (def detected-squares (filter some?
66: (map #(rotsquare? (first %) (second %) grid)
67: (partition-corners (all-hashes grid)))))
68:
69: (def sol-2
70: (count (set (map set detected-squares))))