feat: solve day 4 puzzle 1

This commit is contained in:
2025-12-04 12:46:22 +01:00
parent d0cc52fbda
commit 3fc4ac1700
5 changed files with 64 additions and 3 deletions

View File

@@ -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: 6/24 #### Stars: 7/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>: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>||| |8<br>|9<br>|10<br>|11<br>|12<br>|||
<!-- calendar-end --> <!-- calendar-end -->

10
res/examples/day04.txt Normal file
View File

@@ -0,0 +1,10 @@
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.

View File

@@ -12,7 +12,7 @@
"puzzle2": true "puzzle2": true
}, },
"day04": { "day04": {
"puzzle1": false, "puzzle1": true,
"puzzle2": false "puzzle2": false
}, },
"day05": { "day05": {

44
src/day04/puzzle1.lua Normal file
View 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
View File

@@ -0,0 +1,7 @@
local puzzle2 = {}
function puzzle2.solve(input)
return 0
end
return puzzle2