Compare commits
4 Commits
d20e87c963
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 01d59f641a | |||
| efd650f1cd | |||
| 05c49abca3 | |||
| 6b90ca82e3 |
@@ -10,7 +10,7 @@
|
||||
--
|
||||
--
|
||||
--local input_file = "test"
|
||||
local input_file = "full"
|
||||
local input_file = "Day4/full"
|
||||
local warehouse = {}
|
||||
|
||||
local row = 0
|
||||
|
||||
94
2025/Day5/Day5.lua
Normal file
94
2025/Day5/Day5.lua
Normal 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
1174
2025/Day5/full
Normal file
File diff suppressed because it is too large
Load Diff
11
2025/Day5/test
Normal file
11
2025/Day5/test
Normal file
@@ -0,0 +1,11 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
@@ -23,3 +23,11 @@ This is mostly so I can keep notes on my progress, in single markdown format so
|
||||
## Lessons fourth day
|
||||
|
||||
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
22
2025/autorun.lua
Normal 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,
|
||||
})
|
||||
Reference in New Issue
Block a user