feat: solve day 3 puzzle 1
This commit is contained in:
@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
|
|||||||
## Progress
|
## Progress
|
||||||
|
|
||||||
<!-- calendar-start -->
|
<!-- calendar-start -->
|
||||||
#### Stars: 4/24
|
#### Stars: 5/24
|
||||||
|
|
||||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
|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:|4<br>|5<br>|6<br>|7<br>|
|
||||||
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
||||||
<!-- calendar-end -->
|
<!-- calendar-end -->
|
||||||
|
|||||||
4
res/examples/day03.txt
Normal file
4
res/examples/day03.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
"puzzle2": true
|
"puzzle2": true
|
||||||
},
|
},
|
||||||
"day03": {
|
"day03": {
|
||||||
"puzzle1": false,
|
"puzzle1": true,
|
||||||
"puzzle2": false
|
"puzzle2": false
|
||||||
},
|
},
|
||||||
"day04": {
|
"day04": {
|
||||||
|
|||||||
41
src/day03/puzzle1.lua
Normal file
41
src/day03/puzzle1.lua
Normal 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
|
||||||
7
src/day03/puzzle2.lua
Normal file
7
src/day03/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
local puzzle2 = {}
|
||||||
|
|
||||||
|
function puzzle2.solve(input)
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
return puzzle2
|
||||||
Reference in New Issue
Block a user