Compare commits
1 Commits
efd650f1cd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 01d59f641a |
@@ -2,10 +2,17 @@
|
|||||||
print("Starting Day 5...")
|
print("Starting Day 5...")
|
||||||
|
|
||||||
local input_file = "Day5/full"
|
local input_file = "Day5/full"
|
||||||
|
local DEBUG = false
|
||||||
local Freshness = {
|
local Freshness = {
|
||||||
Range = {},
|
Range = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function debug(msg)
|
||||||
|
if DEBUG then
|
||||||
|
print(msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Freshness.add(R)
|
function Freshness.add(R)
|
||||||
local S, E = string.match(R, "(%d+)-(%d+)")
|
local S, E = string.match(R, "(%d+)-(%d+)")
|
||||||
local Set = { tonumber(S), tonumber(E) }
|
local Set = { tonumber(S), tonumber(E) }
|
||||||
@@ -28,6 +35,47 @@ function Freshness.check(value)
|
|||||||
return false
|
return false
|
||||||
end
|
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 InputMode = true
|
||||||
local FreshCounter = 0
|
local FreshCounter = 0
|
||||||
for line in io.lines(input_file) do
|
for line in io.lines(input_file) do
|
||||||
@@ -37,10 +85,10 @@ for line in io.lines(input_file) do
|
|||||||
Freshness.add(line)
|
Freshness.add(line)
|
||||||
else
|
else
|
||||||
if Freshness.check(line) then
|
if Freshness.check(line) then
|
||||||
print("Fresh Item " .. line)
|
|
||||||
FreshCounter = FreshCounter + 1
|
FreshCounter = FreshCounter + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Found Fresh Ingredients " .. FreshCounter)
|
print("Found Fresh Ingredients " .. FreshCounter)
|
||||||
|
print(string.format("Found IDs %18.0f", Freshness.CalculateIDs()))
|
||||||
|
|||||||
@@ -23,3 +23,11 @@ This is mostly so I can keep notes on my progress, in single markdown format so
|
|||||||
## 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user