From 0add2ecd0304e16675995e75f3ee90436e29a5aa Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sat, 6 Dec 2025 11:47:03 +0100 Subject: [PATCH] feat: solve day 6 puzzle 2 --- README.md | 4 ++-- res/stats.json | 2 +- src/day06/puzzle2.lua | 53 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8083c96..59cf567 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: 11/24 +#### Stars: 12/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star::star:|6
:star:|7
| +|1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star::star:|6
:star::star:|7
| |8
|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index 46ad079..73b3cfe 100644 --- a/res/stats.json +++ b/res/stats.json @@ -21,7 +21,7 @@ }, "day06": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day07": { "puzzle1": false, diff --git a/src/day06/puzzle2.lua b/src/day06/puzzle2.lua index 2f4bc9a..b761383 100644 --- a/src/day06/puzzle2.lua +++ b/src/day06/puzzle2.lua @@ -1,7 +1,58 @@ +local strings = require "cc.strings" local puzzle2 = {} +function puzzle2.parseNumbers(lines) + local numbers = {} + local n = #lines + + for i, line in ipairs(lines) do + if i ~= n then + for j=1, #line do + local c = line:sub(j, j) + if #numbers < j then + table.insert(numbers, {}) + end + if c ~= " " then + table.insert(numbers[j], c) + end + end + end + end + + local problems = {{}} + for _, num in ipairs(numbers) do + if #num == 0 then + table.insert(problems, {}) + else + local value = tonumber(table.concat(num, "")) + table.insert(problems[#problems], value) + end + end + return problems +end + function puzzle2.solve(input) - return 0 + local lines = strings.split(input, "\n") + local values = puzzle2.parseNumbers(lines) + local ops = strings.split(lines[#lines], "%s+") + + local total = 0 + for p, numbers in ipairs(values) do + local op = ops[p] + local value = 0 + if op == "*" then + value = 1 + end + for _, num in ipairs(numbers) do + if op == "+" then + value = value + num + else + value = value * num + end + end + total = total + value + end + return total end return puzzle2