From 66ed753649cf85cfba2b3361a9e58c3c5baf8db3 Mon Sep 17 00:00:00 2001 From: Logan G Date: Mon, 4 Dec 2023 17:49:23 -0700 Subject: [PATCH] Uploaded the real Day 2 Part 2 (real) --- day2/d2p2.lua | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/day2/d2p2.lua b/day2/d2p2.lua index bb13b7b..dece579 100644 --- a/day2/d2p2.lua +++ b/day2/d2p2.lua @@ -1,26 +1,20 @@ ---@name aoc2023d2p1 +--@name aoc2023d2p2 --@author logan2611 --@client --- Loads the input data into a table games = string.explode("\n", string.trim(file.read("aoc2023d2input.txt"))) sum = 0 -max = { - red = 12, - green = 13, - blue = 14 -} - -print(max["red"]) - for i, v in ipairs(games) do - local isValid = true - - -- Get rid of the useless index (Lua is 1 indexed, so this works out really nicely) local curGame = string.gsub(string.trim(v),"^Game .*: ","") + local min = { + red = 0, + green = 0, + blue = 0 + } + local pulls = string.explode("; ", string.trim(curGame)) for _, pull in ipairs(pulls) do local blocks = string.explode(", ", string.trim(pull)) @@ -29,18 +23,14 @@ for i, v in ipairs(games) do local num = tonumber(block_[1]) local color = block_[2] - --print("Game "..i.." ("..pull..") "..num.." "..color) - - if num > max[color] then - print(num) - isValid = false + if min[color] < num then + min[color] = num end end end - if isValid then - sum = sum + i - end + local pow = min["red"] * min["green"] * min["blue"] + sum = sum + pow end print(sum) \ No newline at end of file