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
[-] eco_game@discuss.tchncs.de 2 points 17 hours ago

Kotlin

Part 1 was simple enough, for every node, check to the bottom and right in equal distance if nodes are found there, then search for the fourth node at r + offset and c + offset. Initially I was scared this would take long, but it runs instantly so good enough for me.

It's only day 3 and I already took over 24 h for part 2โ€ฆ Not proud, takes roughly 5 seconds to run, looks horrible, but works:
For every node, draw vectors to all nodes "beneath" it (lower rows), then find orthogonal pairs, and for each pair search for the fourth point again. I'm actually reasonably happy with the idea, I assume if you implement this better it may be pretty fast too.

Solution

class Day03 : Puzzle {

    private lateinit var grid: List<List<Boolean>>
    private lateinit var nodes: Set<Pair<Int, Int>>

    override fun readFile() {
        val input = readInputFromFile(2026, 3, false)
        grid = input.lines().filter { it.isNotBlank() }
            .map { line -> line.map { it == '#' } }
        nodes = input.lines().filter { it.isNotBlank() }
            .mapIndexedNotNull { r, row ->
                val points = row.mapIndexedNotNull { c, cell -> if (cell == '#') c else null }
                    .map { c -> r to c }
                return@mapIndexedNotNull points.ifEmpty { null }
            }.flatten()
            .toSet()
    }

    override fun solvePartOne(): String {
        return nodes.flatMap { (r, c) -> findSquares(r, c) }.toSet().size.toString()
    }

    override fun solvePartTwo(): String {
        val squares = mutableSetOf<Square>()
        for ((nr, nc) in nodes) {
            val vectors = nodes.filter { (r, _) -> r > nr }
                .map { (r, c) -> r - nr to c - nc }
            vectors.mapNotNull { (vr, vc) ->
                    val orth = vectors.filter { (dr, dc) -> dr * vr + dc * vc == 0 && dr * dr + dc * dc == vr * vr + vc * vc }
                    if (orth.size > 1) throw IllegalStateException("orthogonal count is over 1: $orth")
                    return@mapNotNull if (orth.isEmpty()) null else setOf((vr to vc), orth.first())
                }.toSet()
                .forEach { set ->
                    val corner = set.reduce { (ar, ac), (br, bc) -> nr + ar + br to nc + ac + bc }
                    if (corner in nodes) {
                        squares.add(Square(
                            Point(nr, nc),
                            Point(nr + set.first().first, nc + set.first().second),
                            Point(nr + set.last().first, nc + set.last().second),
                            Point(corner.first, corner.second),
                        ))
                    }
                }
            squares.addAll(findSquares(nr, nc)) // add normal squares too
        }
        return squares.size.toString()
    }

    private fun findSquares(r: Int, c: Int): List<Square> {
        return (1..<min(grid.size - r, grid[r].size - c)) // possible step width, towards bottom right
            .map { it to listOf(grid[r][c + it], grid[r + it][c], grid[r + it][c + it]) } // map to grid points
            .filter { corners -> corners.second.all { it } } // only take offsets where the other 3 points are corners
            .map { (offset, _) -> Square(r, c, offset) }     // map offset -> 4 corners -> square
    }

    data class Square(val a: Point, val b: Point, val c: Point, val d: Point) {
        constructor(r: Int, c: Int, offset: Int) : this(
            Point(r, c),
            Point(r, c + offset),
            Point(r + offset, c),
            Point(r + offset, c + offset),
        )
    }
}

full code on Codeberg

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