feat: solve day 4 puzzle 1
This commit is contained in:
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