From 01d59f641ac47e469df02f9393dc4b21e23162d5 Mon Sep 17 00:00:00 2001 From: Bas Grolleman Date: Fri, 5 Dec 2025 09:38:16 +0100 Subject: [PATCH] Day5 Second puzzle --- 2025/' | 2 ++ 2025/Day5/Day5.lua | 50 +++++++++++++++++++++++++++++++++++++++++++++- 2025/README.md | 8 ++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 2025/' diff --git a/2025/' b/2025/' new file mode 100644 index 0000000..d1ffaf6 --- /dev/null +++ b/2025/' @@ -0,0 +1,2 @@ +-- Using Autorun now +print("Starting Day 5...") diff --git a/2025/Day5/Day5.lua b/2025/Day5/Day5.lua index 5f80c9c..2e63281 100644 --- a/2025/Day5/Day5.lua +++ b/2025/Day5/Day5.lua @@ -2,10 +2,17 @@ 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) } @@ -28,6 +35,47 @@ function Freshness.check(value) 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 @@ -37,10 +85,10 @@ for line in io.lines(input_file) do Freshness.add(line) else if Freshness.check(line) then - print("Fresh Item " .. line) FreshCounter = FreshCounter + 1 end end end print("Found Fresh Ingredients " .. FreshCounter) +print(string.format("Found IDs %18.0f", Freshness.CalculateIDs())) diff --git a/2025/README.md b/2025/README.md index a1713de..0c62498 100644 --- a/2025/README.md +++ b/2025/README.md @@ -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 + + +