feat: solve day 4 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: 6/24
|
||||
#### Stars: 7/24
|
||||
|
||||
|Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|
||||
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|
||||
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>|5<br>|6<br>|7<br>|
|
||||
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star:|5<br>|6<br>|7<br>|
|
||||
|8<br>|9<br>|10<br>|11<br>|12<br>|||
|
||||
<!-- calendar-end -->
|
||||
|
||||
10
res/examples/day04.txt
Normal file
10
res/examples/day04.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
@@ -12,7 +12,7 @@
|
||||
"puzzle2": true
|
||||
},
|
||||
"day04": {
|
||||
"puzzle1": false,
|
||||
"puzzle1": true,
|
||||
"puzzle2": false
|
||||
},
|
||||
"day05": {
|
||||
|
||||
44
src/day04/puzzle1.lua
Normal file
44
src/day04/puzzle1.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
local utils = require "utils"
|
||||
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.inGrid(x, y, w, h)
|
||||
return 1 <= x and x <= w and 1 <= y and y <= h
|
||||
end
|
||||
|
||||
function puzzle1.isAccessible(lines, w, h, x, y)
|
||||
local neighbors = 0
|
||||
for dy=-1, 1 do
|
||||
for dx=-1, 1 do
|
||||
if dx ~=0 or dy ~= 0 then
|
||||
local x2 = x + dx
|
||||
local y2 = y + dy
|
||||
if puzzle1.inGrid(x2, y2, w, h) then
|
||||
if lines[y2]:sub(x2, x2) == "@" then
|
||||
neighbors = neighbors + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return neighbors < 4
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local lines = utils.splitLines(input)
|
||||
local accessible = 0
|
||||
|
||||
local h = #lines
|
||||
local w = #lines[1]
|
||||
|
||||
for y, line in ipairs(lines) do
|
||||
for x=1, #line do
|
||||
if line:sub(x, x) == "@" and puzzle1.isAccessible(lines, w, h, x, y) then
|
||||
accessible = accessible + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return accessible
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
7
src/day04/puzzle2.lua
Normal file
7
src/day04/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