Day 2 Part 2 (Lua [StarfallEx])
This commit is contained in:
parent
16b2612367
commit
8f671634d1
1 changed files with 46 additions and 0 deletions
46
day2/d2p2.lua
Normal file
46
day2/d2p2.lua
Normal file
|
@ -0,0 +1,46 @@
|
|||
--@name aoc2023d2p1
|
||||
--@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 pulls = string.explode("; ", string.trim(curGame))
|
||||
for _, pull in ipairs(pulls) do
|
||||
local blocks = string.explode(", ", string.trim(pull))
|
||||
for _, block in ipairs(blocks) do
|
||||
local block_ = string.explode(" ", block)
|
||||
local num = tonumber(block_[1])
|
||||
local color = block_[2]
|
||||
|
||||
--print("Game "..i.." ("..pull..") "..num.." "..color)
|
||||
|
||||
if num > max[color] then
|
||||
print(num)
|
||||
isValid = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if isValid then
|
||||
sum = sum + i
|
||||
end
|
||||
end
|
||||
|
||||
print(sum)
|
Loading…
Reference in a new issue