Uploaded the real Day 2 Part 2 (real)

This commit is contained in:
Logan G 2023-12-04 17:49:23 -07:00
parent 567d472930
commit 66ed753649
Signed by: logan
GPG key ID: E328528C921E7A7A

View file

@ -1,26 +1,20 @@
--@name aoc2023d2p1 --@name aoc2023d2p2
--@author logan2611 --@author logan2611
--@client --@client
-- Loads the input data into a table
games = string.explode("\n", string.trim(file.read("aoc2023d2input.txt"))) games = string.explode("\n", string.trim(file.read("aoc2023d2input.txt")))
sum = 0 sum = 0
max = {
red = 12,
green = 13,
blue = 14
}
print(max["red"])
for i, v in ipairs(games) do 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 curGame = string.gsub(string.trim(v),"^Game .*: ","")
local min = {
red = 0,
green = 0,
blue = 0
}
local pulls = string.explode("; ", string.trim(curGame)) local pulls = string.explode("; ", string.trim(curGame))
for _, pull in ipairs(pulls) do for _, pull in ipairs(pulls) do
local blocks = string.explode(", ", string.trim(pull)) local blocks = string.explode(", ", string.trim(pull))
@ -29,18 +23,14 @@ for i, v in ipairs(games) do
local num = tonumber(block_[1]) local num = tonumber(block_[1])
local color = block_[2] local color = block_[2]
--print("Game "..i.." ("..pull..") "..num.." "..color) if min[color] < num then
min[color] = num
if num > max[color] then
print(num)
isValid = false
end end
end end
end end
if isValid then local pow = min["red"] * min["green"] * min["blue"]
sum = sum + i sum = sum + pow
end
end end
print(sum) print(sum)