From 30693ed0690663379779408292b86a7d828cd3b9 Mon Sep 17 00:00:00 2001 From: Bas Grolleman Date: Mon, 1 Dec 2025 17:26:56 +0100 Subject: [PATCH] Cleanup code a bit and add comments --- 2025/Day1/Day1_final.lua | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 2025/Day1/Day1_final.lua diff --git a/2025/Day1/Day1_final.lua b/2025/Day1/Day1_final.lua new file mode 100755 index 0000000..7ceed51 --- /dev/null +++ b/2025/Day1/Day1_final.lua @@ -0,0 +1,60 @@ +#!/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 + ) +)