Day 4 done

This commit is contained in:
2025-12-04 08:16:42 +01:00
parent f159a0fee6
commit febe80cfb5
5 changed files with 341 additions and 0 deletions

108
2025/Day4/Day4_first.lua Executable file
View File

@@ -0,0 +1,108 @@
#!/usr/bin/lua
--Advent of Code - 2025 - Day 4
--
--https://adventofcode.com/2025/day/4
-- Problem, find space for forklift to move
-- Tasks
-- [ ] Load Paper roll in array
-- [ ] Walk through Array finding spots
--
--
--local input_file = "test"
local input_file = "full"
local warehouse = {}
local row = 0
local col = 0
for line in io.lines(input_file) do
row = row + 1
warehouse[row] = {}
col = 0
for location in line:gmatch(".") do
col = col + 1
warehouse[row][col] = location
end
end
function space_for_forklift(warehouse, x, y)
-- I'm making the assuption here that all rows have the same columns
-- normally I would say write a selfcheck to confirm input is sound
local rows = table.getn(warehouse)
local columns = table.getn(warehouse[1])
local spaces = 0
if warehouse[x][y] ~= "@" then
-- No paper roll, so skip
--print("warehouse check " .. x .. "," .. y .. " no paper roll")
return false
end
local freespace = 0
local rolls = 0
for rowskim = -1, 1 do
if warehouse[x + rowskim] == nil then
freespace = freespace + 3
else
for colskim = -1, 1 do
local w = warehouse[x + rowskim][y + colskim]
--print("checking " .. (x + rowskim) .. "," .. (y + colskim) .. "=" .. w)
if not (rowskim == 0 and colskim == 0) then
if w == "@" or w == "X" then
rolls = rolls + 1
else
freespace = freespace + 1
end
end
end
end
end
if rolls < 4 then
--print("warehouse check " .. x .. "," .. y .. " found space")
warehouse[x][y] = "X"
return true
else
--print("warehouse check " .. x .. "," .. y .. " no space")
return false
end
end
function remove_rolls(warehouse)
local maxrows = table.getn(warehouse)
local maxcolumns = table.getn(warehouse[1])
for r = 1, maxrows do
for c = 1, maxcolumns do
if warehouse[r][c] == "X" then
warehouse[r][c] = "."
end
end
end
end
-- print(space_for_forklift(warehouse, 1, 1))
-- print(space_for_forklift(warehouse, 1, 3))
-- print(space_for_forklift(warehouse, 2, 1))
-- print(space_for_forklift(warehouse, 3, 7))
function countrolls(warehouse)
local maxrows = table.getn(warehouse)
local maxcolumns = table.getn(warehouse[1])
local forkliftaccess = 0
for r = 1, maxrows do
for c = 1, maxcolumns do
if space_for_forklift(warehouse, r, c) then
forkliftaccess = forkliftaccess + 1
end
end
end
return forkliftaccess
end
totalrolls = 0
for I = 1, 100 do
cr = countrolls(warehouse)
totalrolls = totalrolls + cr
print("Loop " .. I .. ": " .. cr .. " Rolls removed")
remove_rolls(warehouse)
end
print("Total rolls removed " .. totalrolls)