23

Day 3: Foggy Forest

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] ismail@programming.dev 4 points 1 day ago

TXR

The idea behind solving part 1 is to consider every horizontal pair of hash characters as potentially forming the top edge of a square. The assumption is proven if an aligned pair of hash chars exists in a lower row.

Part 1

(defun day03 ()
  (let ((grid (file-get-lines "03-input")))
    (put-line `1: @(pt1 grid)`)
    (put-line `2: @(pt2 grid)`)))

(defun pt1 (grid)
  (let ((side (len grid))
        (count 0))
    (each ((line grid)
           (row 0..side))
      (let ((cols (let ((pos -1))
                    (gun (set pos (search-str line "#" (succ pos)))))))
        (each ((left (conses cols)))
          (each ((right (cdr left)))
            (let ((base (+ row (- right (car left)))))
              (when (and (< base side)
                         (eq #\# (mref grid base (car left)))
                         (eq #\# (mref grid base right)))
                (inc count)))))))
    count))

This solution to part 2 is as brutal as brute-force can be. Every rotation of every possible square is checked and the tally is increased if hash chars are present at its corners.

(defun pt2 (grid)
  (let ((side (len grid))
        (count 0))
    (each ((gap 1..(succ side)))
      (each* ((x1 0..(- side gap))
              (x2 (+ x1 gap)))
        (each* ((y1 0..(- side gap))
                (y2 (+ y1 gap)))
          (each ((offset 0..gap))
            (macrolet ((tree-at (x y)
                         ^(eq #\# (mref grid ,y ,x))))
              (when (and (tree-at (+ x1 offset) y1)
                         (tree-at x2 (+ y1 offset))
                         (tree-at (- x2 offset) y2)
                         (tree-at x1 (- y2 offset)))
                (inc count)))))))
    count))
this post was submitted on 03 Dec 2026
23 points (100.0% liked)

Advent Of Code

1278 readers
56 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS