From bd36baa1ec6f7402a41e8475c09e7306b4f94ba7 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 5 Dec 2025 11:55:54 +0100 Subject: [PATCH] feat: solve day 5 puzzle 2 --- README.md | 4 ++-- res/stats.json | 2 +- src/day05/puzzle2.lua | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ea29d06..50e8134 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git ## Progress -#### Stars: 9/24 +#### Stars: 10/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star:|6
|7
| +|1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star::star:|6
|7
| |8
|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index 4c24ab6..0c99a6b 100644 --- a/res/stats.json +++ b/res/stats.json @@ -17,7 +17,7 @@ }, "day05": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day06": { "puzzle1": false, diff --git a/src/day05/puzzle2.lua b/src/day05/puzzle2.lua index 2f4bc9a..a165f25 100644 --- a/src/day05/puzzle2.lua +++ b/src/day05/puzzle2.lua @@ -1,7 +1,47 @@ +local strings = require "cc.strings" local puzzle2 = {} function puzzle2.solve(input) - return 0 + local lines = strings.split(input, "\n") + local bounds = {} + + for _, line in ipairs(lines) do + if line == "" then + break + end + local min, max = line:match("(%d+)%-(%d+)") + min = tonumber(min) + max = tonumber(max) + table.insert(bounds, {i=min, type="start"}) + table.insert(bounds, {i=max, type="end"}) + end + + table.sort(bounds, function (a, b) + if a.i == b.i then + return a.type == "start" and b.type == "end" + end + return a.i < b.i + end) + + local totalFresh = 0 + local min = 0 + local balance = 0 + + for _, bound in ipairs(bounds) do + if bound.type == "start" then + if balance == 0 then + min = bound.i + end + balance = balance + 1 + else + balance = balance - 1 + if balance == 0 then + totalFresh = totalFresh + bound.i - min + 1 + end + end + end + + return totalFresh end return puzzle2