feat: solve day 2 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
|
||||
|
||||
<!-- calendar-start -->
|
||||
#### Stars: 2/24
|
||||
#### Stars: 3/24
|
||||
|
||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
||||
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
||||
|1<br>:star::star:|2<br>|3<br>|4<br>|5<br>|6<br>|7<br>|
|
||||
|1<br>:star::star:|2<br>:star:|3<br>|4<br>|5<br>|6<br>|7<br>|
|
||||
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
3
res/examples/day02.txt
Normal file
3
res/examples/day02.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
|
||||
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
|
||||
824824821-824824827,2121212118-2121212124
|
||||
@@ -4,7 +4,7 @@
|
||||
"puzzle2": true
|
||||
},
|
||||
"day02": {
|
||||
"puzzle1": false,
|
||||
"puzzle1": true,
|
||||
"puzzle2": false
|
||||
},
|
||||
"day03": {
|
||||
|
||||
38
src/day02/puzzle1.lua
Normal file
38
src/day02/puzzle1.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.splitRange(range)
|
||||
local parts = utils.split(range, "-")
|
||||
return tonumber(parts[1]), tonumber(parts[2])
|
||||
end
|
||||
|
||||
function puzzle1.isValid(id)
|
||||
local str = tostring(id)
|
||||
if #str % 2 == 1 then
|
||||
return true
|
||||
end
|
||||
local mid = #str / 2
|
||||
return str:sub(1, mid) ~= str:sub(mid + 1)
|
||||
end
|
||||
|
||||
function puzzle1.countInvalids(range)
|
||||
local min, max = puzzle1.splitRange(range)
|
||||
local total = 0
|
||||
for i=min, max do
|
||||
if not puzzle1.isValid(i) then
|
||||
total = total + i
|
||||
end
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local ranges = utils.split(input, ",")
|
||||
local total = 0
|
||||
for _, range in ipairs(ranges) do
|
||||
total = total + puzzle1.countInvalids(range)
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
7
src/day02/puzzle2.lua
Normal file
7
src/day02/puzzle2.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
local puzzle2 = {}
|
||||
|
||||
function puzzle2.solve(input)
|
||||
return 0
|
||||
end
|
||||
|
||||
return puzzle2
|
||||
@@ -78,14 +78,18 @@ function utils.waitForKey(targetKey)
|
||||
end
|
||||
end
|
||||
|
||||
function utils.splitLines(data)
|
||||
function utils.split(data, sep)
|
||||
local t = {}
|
||||
for str in string.gmatch(data, "([^\n]+)") do
|
||||
for str in string.gmatch(data, "([^" .. sep .. "]+)") do
|
||||
table.insert(t, str)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function utils.splitLines(data)
|
||||
return utils.split(data, "\n")
|
||||
end
|
||||
|
||||
function utils.round(x)
|
||||
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user