From e1623ac0fa2967d1871b0f3a9fe341024194754d Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 1 Dec 2025 12:08:44 +0100 Subject: [PATCH] feat: solve day 1 puzzle 2 --- README.md | 4 ++-- res/stats.json | 2 +- src/day01/puzzle2.lua | 26 +++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4139e83..6e0b386 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: 1/24 +#### Stars: 2/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| -|1
:star:|2
|3
|4
|5
|6
|7
| +|1
:star::star:|2
|3
|4
|5
|6
|7
| |8
|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index f9e3c2d..08df9c6 100644 --- a/res/stats.json +++ b/res/stats.json @@ -1,7 +1,7 @@ { "day01": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day02": { "puzzle1": false, diff --git a/src/day01/puzzle2.lua b/src/day01/puzzle2.lua index 2f4bc9a..87e2c2b 100644 --- a/src/day01/puzzle2.lua +++ b/src/day01/puzzle2.lua @@ -1,7 +1,31 @@ +local utils = require "utils" local puzzle2 = {} function puzzle2.solve(input) - return 0 + local password = 0 + local cursor = 50 + + local lines = utils.splitLines(input) + for _, line in ipairs(lines) do + local dir = line:sub(1, 1) + local dist = tonumber(line:sub(2)) + if dir == "R" then + cursor = cursor + dist + if cursor > 99 then + password = password + math.floor(cursor / 100) + end + else + cursor = cursor - dist + if cursor <= 0 then + if cursor ~= -dist then + password = password + 1 + end + password = password + math.floor(-cursor / 100) + end + end + cursor = cursor % 100 + end + return password end return puzzle2