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
[-] Hugh@programming.dev 5 points 1 day ago* (last edited 1 day ago)

Lua

Part1 solution: For each line I generated an array of tree pairs by encoding the position of the first tree and distance to another tree as a comma seperated string. Eg. #...# would be encoded as {"1,5"} and ##..# would be {"1,2", "1,5"}. Then for each pair of trees I check if row - width contains a matching string.

Part 1

local pairsTable = {}
local line = io.read("*line")
local forestWidth = #line
local count = 0

function hasMatchingPair(encodedData, rowToCheck)
        if rowToCheck < 1 then
                return false
        end
        local row = pairsTable[rowToCheck]
        for _,data in ipairs(row.data) do
                if encodedData == data then
                        return true
                end
        end
end

local row = 1
while true do
        local positionsRow = {}
        local positions = {}

        for i=1,forestWidth do
                local char = line:sub(i,i)
                if char == '#' then
                        table.insert(positions, i)
                end
        end
        if #positions > 1 then
                for i=1,#positions-1 do
                        for j=i+1,#positions do
                                pos = positions[i]
                                width = positions[j]-positions[i] + 1
                                encodedData = tostring(pos) .. "," .. tostring(width)
                                table.insert(positionsRow, encodedData)
                                if hasMatchingPair(encodedData, row - width + 1) then
                                        count = count + 1
                                end
                        end
                end
        end

        table.insert(pairsTable, {row = row, data = positionsRow})
        line = io.read("*line")
        if line == nil then break end
        row = row + 1
end
print("Total is: " .. count)

Coming from C to Lua is a bit annoying, because a lot of the data structure is abstracted from you, so things don't always work how I expect them to.

Edit: finally finished part 2. My approach was to identify all the trees (a) that are equidistant from two other trees (b,c) AND have a third tree (d) that corresponds to the sum of the vectors to the other two trees. Then I check that |a->d| == |b->c| then I think it is a square...

ngl I found that quite challenging. Good thing I quit my day job.

Elapsed CPU time: 0.682239 seconds

solution

local trees = {}
local line = io.read("*line")
local forestWidth = #line
local squareCount = 0
local row = 1

-- Looks back at visited trees, gets a vectors from current tree to all previous trees
function checkAllPreviousTrees(trees, row, col)
        local distances = {}
        for _,tree in pairs(trees) do
                -- calculate distance to previous tree
                i = tree.pos.row - row
                j = tree.pos.col - col
                dist = i^2 + j^2
                if distances[dist] then
                        table.insert(distances[dist], {row = i, col = j})
                else
                        distances[dist] = {{row = i, col = j}}
                end
        end
        return distances
end

-- Loop over input, hashing tree positions
while true do
        for col=1,forestWidth do
                local distances = {}
                local id
                if line:sub(col,col) == '#' then
                        distances = checkAllPreviousTrees(trees, row, col)
                        id = col + (forestWidth * (row-1))
                        trees[id] = {
                                pos = {row = row, col = col},
                                distances = distances
                        }
                end
        end
        line = io.read("*line")
        if line == nil then break end
        row = row + 1
end

function diagonalsAreEqual(A, B)
        local vecAB = {-A.row + B.row, -A.col + B.col}
        local vecPC = {A.row + B.row, A.col + B.col}
        local magAB = vecAB[1]^2 + vecAB[2]^2
        local magPC = vecPC[1]^2 + vecPC[2]^2
        if magAB == magPC then
                print("Found a square")
                return true
        end
        return false
end

-- iterate over vectors, if two or more vectors have the same magnitude then
-- compare the magnitude of their diagonals
for id,tree in pairs(trees) do
        for distance,vectors in pairs(tree.distances) do
                local pos = tree.pos
                if #vectors > 1 then
                        local diagonal = {}
                        local vectorSum
                        for i=1,#vectors-1 do
                                for j=i+1,#vectors do
                                        diagonal.row = vectors[i].row + vectors[j].row + pos.row
                                        diagonal.col = vectors[i].col + vectors[j].col + pos.col
                                        vectorSum = diagonal.col + (forestWidth * (diagonal.row-1))
                                        -- if there is a tree on the diagonal
                                        if trees[vectorSum] then
                                                if diagonalsAreEqual(vectors[i], vectors[j]) then
                                                        squareCount = squareCount + 1
                                                end
                                        end
                                end
                        end
                end
        end
end

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