From d0cc52fbda5c03e9840cf4bb01e55998c545ce42 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 3 Dec 2025 13:15:45 +0100 Subject: [PATCH] feat: solve day 3 puzzle 2 --- README.md | 4 +-- res/stats.json | 2 +- src/day03/puzzle2.lua | 59 ++++++++++++++++++++++++++++++++++++++++++- src/lib/days.lua | 2 +- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f936d71..60cc3f9 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git ## Progress -#### Stars: 5/24 +#### Stars: 6/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star::star:|2
:star::star:|3
:star:|4
|5
|6
|7
| +|1
:star::star:|2
:star::star:|3
:star::star:|4
|5
|6
|7
| |8
|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index 1987838..359a654 100644 --- a/res/stats.json +++ b/res/stats.json @@ -9,7 +9,7 @@ }, "day03": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day04": { "puzzle1": false, diff --git a/src/day03/puzzle2.lua b/src/day03/puzzle2.lua index 2f4bc9a..2af22d7 100644 --- a/src/day03/puzzle2.lua +++ b/src/day03/puzzle2.lua @@ -1,7 +1,64 @@ +local utils = require("utils") +local puzzle1 = require(SRC_PATH .. "/day03/puzzle1") local puzzle2 = {} +local cache = {} + +function puzzle2.findMaxJoltage(bank, joltages, n, startI) + startI = startI or 1 + local key = bank:sub(startI) .. "-" .. tostring(n) + if cache[key] then + return cache[key] + end + + local maxJoltage = 0 + local maxDigit = 0 + + if startI > #joltages then + return nil + end + + if n == 1 then + for i=startI, #joltages do + if joltages[i] > maxDigit then + maxDigit = joltages[i] + end + end + cache[key] = maxDigit + return maxDigit + end + + local i = startI + while i <= #joltages do + local digit = joltages[i] + if digit > maxDigit then + local maxSuffix = puzzle2.findMaxJoltage(bank, joltages, n - 1, i + 1) + if maxSuffix ~= nil then + local joltage = tonumber(tostring(digit) .. tostring(maxSuffix)) + if joltage > maxJoltage then + maxDigit = digit + maxJoltage = joltage + end + end + end + i = i + 1 + end + if maxJoltage == 0 then + return nil + end + cache[key] = maxJoltage + return maxJoltage +end + function puzzle2.solve(input) - return 0 + local banks = utils.splitLines(input) + local totalJoltage = 0 + for _, bank in ipairs(banks) do + local joltages = puzzle1.bankJoltages(bank) + local max = puzzle2.findMaxJoltage(bank, joltages, 12) + totalJoltage = totalJoltage + max + end + return totalJoltage end return puzzle2 diff --git a/src/lib/days.lua b/src/lib/days.lua index 5ec2439..ef9f84a 100644 --- a/src/lib/days.lua +++ b/src/lib/days.lua @@ -147,7 +147,7 @@ function Day:execPuzzle(puzzleI, data, resultKey) local result = puzzle.solve(data) local t1 = os.epoch("local") print(("(Executed in %.3fs)"):format((t1 - t0) / 1000)) - print("Result:", result) + print(("Result: %.0f"):format(result)) self.results[resultKey] = result self:saveResults() end