Compare commits

...

2 Commits

Author SHA1 Message Date
0add2ecd03 feat: solve day 6 puzzle 2 2025-12-06 11:47:03 +01:00
d9ac0c7a9f feat: solve day 6 puzzle 1 2025-12-06 11:36:03 +01:00
5 changed files with 105 additions and 4 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: 10/24
#### Stars: 12/24
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>|7<br>|
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>:star::star:|7<br>|
|8<br>|9<br>|10<br>|11<br>|12<br>|||
<!-- calendar-end -->

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

@@ -0,0 +1,4 @@
123 328 51 64
45 64 387 23
6 98 215 314
* + * +

View File

@@ -20,8 +20,8 @@
"puzzle2": true
},
"day06": {
"puzzle1": false,
"puzzle2": false
"puzzle1": true,
"puzzle2": true
},
"day07": {
"puzzle1": false,

39
src/day06/puzzle1.lua Normal file
View File

@@ -0,0 +1,39 @@
local strings = require "cc.strings"
local puzzle1 = {}
function puzzle1.solve(input)
local lines = strings.split(input, "\n")
local problems = {}
local n = #lines
local ops = strings.split(lines[n], "%s+")
for i, line in ipairs(lines) do
local j = 1
for num in line:gmatch("(%d+)") do
local op = ops[j]
if i == 1 then
if op == "+" then
table.insert(problems, 0)
else
table.insert(problems, 1)
end
end
local val = tonumber(num)
if op == "+" then
problems[j] = problems[j] + val
else
problems[j] = problems[j] * val
end
j = j + 1
end
end
local total = 0
for _, val in ipairs(problems) do
total = total + val
end
return total
end
return puzzle1

58
src/day06/puzzle2.lua Normal file
View File

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