Compare commits

..

2 Commits

Author SHA1 Message Date
448dbf0ff9 feat: solve day 4 puzzle 2 2025-12-04 12:57:09 +01:00
3fc4ac1700 feat: solve day 4 puzzle 1 2025-12-04 12:46:22 +01:00
5 changed files with 164 additions and 4 deletions

View File

@@ -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: 8/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::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
View File

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

View File

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

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

106
src/day04/puzzle2.lua Normal file
View File

@@ -0,0 +1,106 @@
local utils = require "utils"
local puzzle2 = {}
function puzzle2.inGrid(x, y, w, h)
return 1 <= x and x <= w and 1 <= y and y <= h
end
function puzzle2.countNeighbors(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 puzzle2.inGrid(x2, y2, w, h) then
if lines[y2]:sub(x2, x2) == "@" then
neighbors = neighbors + 1
end
end
end
end
end
return neighbors
end
function puzzle2.copyGrid(grid)
local grid2 = {}
for y, row in ipairs(grid) do
local row2 = {}
for x, n in ipairs(row) do
row2[x] = n
end
grid2[y] = row2
end
return grid2
end
function puzzle2.removeRoll(grid, x, y, w, h)
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 puzzle2.inGrid(x2, y2, w, h) then
if grid[y2][x2] > 0 then
grid[y2][x2] = grid[y2][x2] - 1
end
end
end
end
end
grid[y][x] = -1
end
function puzzle2.removeRolls(grid, w, h)
local removed = 0
local grid2 = puzzle2.copyGrid(grid)
for y, row in ipairs(grid) do
for x, n in ipairs(row) do
if 0 <= n and n < 4 then
puzzle2.removeRoll(grid2, x, y, w, h)
removed = removed + 1
end
end
end
return removed, grid2
end
function puzzle2.solve(input)
local lines = utils.splitLines(input)
local h = #lines
local w = #lines[1]
local grid = {}
for y, line in ipairs(lines) do
local row = {}
for x=1, #line do
local neighbors = -1
if line:sub(x, x) == "@" then
neighbors = puzzle2.countNeighbors(lines, w, h, x, y)
end
row[x] = neighbors
end
grid[y] = row
end
local totalRemoved = 0
local removed = 0
while true do
removed, grid = puzzle2.removeRolls(grid, w, h)
if removed == 0 then
break
end
totalRemoved = totalRemoved + removed
end
return totalRemoved
end
return puzzle2