Compare commits

...

2 Commits

Author SHA1 Message Date
d0cc52fbda feat: solve day 3 puzzle 2 2025-12-03 13:15:45 +01:00
3be58692ab feat: solve day 3 puzzle 1 2025-12-03 09:56:17 +01:00
6 changed files with 114 additions and 5 deletions

View File

@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
## Progress
<!-- calendar-start -->
#### Stars: 4/24
#### Stars: 6/24
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|1<br>:star::star:|2<br>:star::star:|3<br>|4<br>|5<br>|6<br>|7<br>|
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>|5<br>|6<br>|7<br>|
|8<br>|9<br>|10<br>|11<br>|12<br>|||
<!-- calendar-end -->

4
res/examples/day03.txt Normal file
View File

@@ -0,0 +1,4 @@
987654321111111
811111111111119
234234234234278
818181911112111

View File

@@ -8,8 +8,8 @@
"puzzle2": true
},
"day03": {
"puzzle1": false,
"puzzle2": false
"puzzle1": true,
"puzzle2": true
},
"day04": {
"puzzle1": false,

41
src/day03/puzzle1.lua Normal file
View File

@@ -0,0 +1,41 @@
local utils = require("utils")
local puzzle1 = {}
function puzzle1.bankJoltages(bank)
local joltages = {}
for i=1, #bank do
table.insert(joltages, tonumber(bank:sub(i, i)))
end
return joltages
end
function puzzle1.findMaxJoltage(bank)
local maxTens = 0
local maxJoltage = 0
local joltages = puzzle1.bankJoltages(bank)
for i, tens in ipairs(joltages) do
if tens > maxTens then
for j=i+1, #joltages do
local ones = joltages[j]
local joltage = tens * 10 + ones
if joltage > maxJoltage then
maxTens = tens
maxJoltage = joltage
end
end
end
end
return maxJoltage
end
function puzzle1.solve(input)
local banks = utils.splitLines(input)
local totalJoltage = 0
for _, bank in ipairs(banks) do
totalJoltage = totalJoltage + puzzle1.findMaxJoltage(bank)
end
return totalJoltage
end
return puzzle1

64
src/day03/puzzle2.lua Normal file
View File

@@ -0,0 +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)
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

View File

@@ -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