Compare commits

..

6 Commits

Author SHA1 Message Date
01d59f641a Day5 Second puzzle 2025-12-05 09:38:16 +01:00
efd650f1cd Test autorunner on Day4 2025-12-05 07:59:37 +01:00
05c49abca3 Day5 First puzzle get 2025-12-05 07:59:09 +01:00
6b90ca82e3 Create autorun script for neovim 2025-12-05 07:58:53 +01:00
d20e87c963 Update Readme 2025-12-04 21:27:10 +01:00
1785bf5038 Day3 second 2025-12-04 21:26:21 +01:00
8 changed files with 1340 additions and 46 deletions

2
2025/' Normal file
View File

@@ -0,0 +1,2 @@
-- Using Autorun now
print("Starting Day 5...")

View File

@@ -10,51 +10,31 @@ function debug(msg)
end end
function MaxJolt(Battery, Size) function MaxJolt(Battery, Size)
-- TODO - This going to be a rework, I need to loop 12 times debug("Staring Battery Check [" .. Battery .. "] Size [" .. Size .. "]")
local BatteryDigits = {}
for I = 1, tonumber(Size) do -- Load Battery into Array
BatteryDigits[I] = 0 local BatteryTable = {}
local BatterySize = string.len(Battery)
for Cell in Battery:gmatch(".") do
table.insert(BatteryTable, tonumber(Cell))
end end
-- lastdigit = tonumber(string.sub(Battery, -1)) local Digits = {}
-- debug("lastdigit " .. lastdigit) local Start = 1
--
-- BatteryNoLast = string.sub(Battery, 0, -2) -- Outer loop for Digits
-- BatteryNoLast:gsub(".", function(c)
-- table.insert(b, c)
-- return c
-- end)
--
-- for key, value in pairs(b) do
-- checkdigit = tonumber(value)
-- if firstdigit < checkdigit then
-- firstdigit = checkdigit
-- end
-- end
--
-- local passedfirst = false
-- for key, value in pairs(b) do
-- checkdigit = tonumber(value)
-- if passedfirst and seconddigit < checkdigit then
-- seconddigit = checkdigit
-- end
-- if checkdigit == firstdigit then
-- passedfirst = true
-- end
-- end
--
-- if seconddigit < lastdigit then
-- seconddigit = lastdigit
-- end
--
-- debug("First " .. firstdigit)
-- debug("Second " .. seconddigit)
-- return tostring(firstdigit) .. tostring(seconddigit)
local BatteryString = ""
for I = 1, tonumber(Size) do for I = 1, tonumber(Size) do
BatteryString = BatteryString .. tostring(BatteryDigits[I]) debug("Checking digits " .. Start .. " till " .. (BatterySize - (Size - I)))
Digits[I] = 0
for Check = Start, BatterySize - (Size - I) do
if Digits[I] < BatteryTable[Check] then
Digits[I] = BatteryTable[Check]
Start = Check + 1
end
end
end end
return BatteryString
return table.concat(Digits)
end end
function TestMaxJolt(Battery, Test, Size) function TestMaxJolt(Battery, Test, Size)
@@ -71,11 +51,13 @@ TestMaxJolt("987654321111111", "987654321111", 12)
TestMaxJolt("811111111111119", "811111111119", 12) TestMaxJolt("811111111111119", "811111111119", 12)
TestMaxJolt("234234234234278", "434234234278", 12) TestMaxJolt("234234234234278", "434234234278", 12)
TestMaxJolt("818181911112111", "888911112111", 12) TestMaxJolt("818181911112111", "888911112111", 12)
--
print("# Calculate MaxJolt") print("# Calculate MaxJolt")
local TotalJolt = 0 local TotalJolt = 0
--
for battery_line in io.lines("full_input") do for battery_line in io.lines("full_input") do
TotalJolt = TotalJolt + MaxJolt(battery_line) local Jolt = MaxJolt(battery_line, 12)
print(Jolt)
TotalJolt = TotalJolt + Jolt
end end
print("Total " .. TotalJolt) print("Total " .. string.format("%18.0f", TotalJolt))

View File

@@ -10,7 +10,7 @@
-- --
-- --
--local input_file = "test" --local input_file = "test"
local input_file = "full" local input_file = "Day4/full"
local warehouse = {} local warehouse = {}
local row = 0 local row = 0

94
2025/Day5/Day5.lua Normal file
View File

@@ -0,0 +1,94 @@
-- Using Autorun now
print("Starting Day 5...")
local input_file = "Day5/full"
local DEBUG = false
local Freshness = {
Range = {},
}
function debug(msg)
if DEBUG then
print(msg)
end
end
function Freshness.add(R)
local S, E = string.match(R, "(%d+)-(%d+)")
local Set = { tonumber(S), tonumber(E) }
table.insert(Freshness.Range, Set)
end
function Freshness.show()
for k, v in pairs(Freshness.Range) do
print(string.format("%d-%d", v[1], v[2]))
end
end
function Freshness.check(value)
cv = tonumber(value)
for k, v in pairs(Freshness.Range) do
if cv >= v[1] and cv <= v[2] then
return true
end
end
return false
end
function Freshness.CalculateIDs()
-- Copy and Sort
table.sort(Freshness.Range, function(a, b)
return a[1] < b[1]
end)
local IDTable = {}
local count = 0
for kadd, vadd in pairs(Freshness.Range) do
local merged = false
debug("Checking " .. vadd[1] .. "-" .. vadd[2])
for kcheck, vcheck in pairs(IDTable) do
-- Check Lower
debug(" Compare " .. vcheck[1] .. "-" .. vcheck[2])
if vadd[1] >= vcheck[1] and vadd[1] <= vcheck[2] then
if vadd[2] > vcheck[2] then
debug(" Lower number inside range and higher number higher")
IDTable[kcheck][2] = vadd[2]
end
merged = true
end
-- Check higher
if vadd[2] >= vcheck[1] and vadd[2] <= vcheck[2] then
if vadd[1] < vcheck[1] then
debug(" Higher number inside range and lower number lower")
IDTable[kcheck][1] = vadd[1]
end
merged = true
end
end
if not merged then
table.insert(IDTable, vadd)
end
end
local countIDs = 0
for k, v in pairs(IDTable) do
countIDs = countIDs + (v[2] - v[1]) + 1
print(string.format("Range %d-%d = %d Total %d", v[1], v[2], (v[2] - v[1] + 1), countIDs))
end
return countIDs
end
local InputMode = true
local FreshCounter = 0
for line in io.lines(input_file) do
if line == "" then
InputMode = false
elseif InputMode then
Freshness.add(line)
else
if Freshness.check(line) then
FreshCounter = FreshCounter + 1
end
end
end
print("Found Fresh Ingredients " .. FreshCounter)
print(string.format("Found IDs %18.0f", Freshness.CalculateIDs()))

1174
2025/Day5/full Normal file

File diff suppressed because it is too large Load Diff

11
2025/Day5/test Normal file
View File

@@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32

View File

@@ -18,7 +18,16 @@ This is mostly so I can keep notes on my progress, in single markdown format so
## Lessons third day ## Lessons third day
1. Can't win em all 1. Can't win em all
2. Code is much shorter because I'm getting better with Lua, yay
## Lessons fourth day ## Lessons fourth day
1. Need about an hour to do both, but also speeding up 1. Need about an hour to do both, but also speeding up
## Lessons fifth day
1. Lua has a maximum table size, so no brute forcing :)
2. When doing any type of comparing, a good pre-sort helps

22
2025/autorun.lua Normal file
View File

@@ -0,0 +1,22 @@
-- Use this to get the buffer ID where you want the output
-- echo nvim_get_current_buf()
vim.api.nvim_create_autocmd("BufWritePost", {
group = vim.api.nvim_create_augroup("AutoRunCode", { clear = true }),
pattern = "*.lua",
callback = function()
local bufnr = 21
vim.fn.jobstart({ "lua", "Day5/Day5.lua" }, {
stdout_buffered = true,
on_stdout = function(_, data)
if data then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, data)
end
end,
on_stderr = function(_, data)
if data then
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, data)
end
end,
})
end,
})