Files
adventofcode/2025/Day1/Day1_final.lua

61 lines
1.1 KiB
Lua
Executable File

#!/bin/lua
-- Cleaned up version of my code
--
-- Author: Bas Grolleman
--
-- Variables
local Dial = 50
local CountZero = 0
local CountClick = 0
local Debug = false
for line in io.lines() do
-- Extract variables from line (Lacks error handling)
local Direction, Count = line:match("^(%w)(%d+)$")
-- Count clicks on higher then 100 number
CountClick = CountClick + math.floor(Count / 100)
Count = Count - (math.floor(Count / 100) * 100)
-- L is negative, so make counter reflect that
if Direction == "L" then
Count = 0 - Count
end
-- Touch that dial
if Dial == 0 then
Dial = Dial + Count
else
Dial = Dial + Count
-- Check if dial past 0
if Dial < 0 or Dial > 100 then
CountClick = CountClick + 1
end
end
-- Set Dial within 0-99
Dial = Dial % 100
-- Count 0
if Dial == 0 then
CountZero = CountZero + 1
end
-- Debug Lines when needed
if Debug then
print("Direction=" .. Direction .. " Count=" .. Count .. " Dial=" .. Dial .. " CountClick=" .. CountClick)
end
end
-- Print results
print(
string.format(
"Dial %s | Countzero %s | Countclick %s\n\nAnswer %s",
Dial,
CountZero,
CountClick,
CountZero + CountClick
)
)