advent-of-code/12022/16/16.lua

100 lines
2.2 KiB
Lua

io.input("test")
--io.input("input")
function newValve( r, ids )
return { rate = tonumber(r), ids = ids , open = false, valves = {}, idsAt = {} }
end
local map = {}
for line in io.lines() do
local pat = "(%u%u) [%l ]+=(%d+); [%l ]+([%u, ]+)$"
local valve, rate, list = string.match(line, pat)
local outIds = {}
for v in string.gmatch(list, "(%u%u)") do
table.insert( outIds, v )
end
map[valve] = newValve( rate, outIds )
print(valve, rate, list)
end
-- maybe not needed?
for k, v in pairs(map) do
for _, id in ipairs(v.ids) do
table.insert( v.valves, map[id] )
end
end
function releasedPressure()
local total = 0
for k, valve in pairs(map) do
total = total + ( valve.open and valve.rate or 0 )
end
return total
end
function contains( array, element )
for _, e in ipairs(array) do
if e == element then return true end
end
return false
end
function idsAtSteps( id, steps )
if steps == 1 then
return map[id].ids
else
if map[id].idsAt[steps] then
return map[id].idsAt[steps]
end
local ids = {}
for _, id in ipairs(map[id].ids) do
local idsDeep = idsAtSteps( id, steps-1 )
for _, idD in ipairs(idsDeep) do
if not contains( ids, idD) then
table.insert( ids, idD )
end
end
end
map[id].idsAt[steps] = ids
return ids
end
end
function potentialRateFromAt( id, steps )
local steps = steps or 1
local valve = map[id]
local candidates = {}
for _, v in ipairs( valve.valves ) do
if not v.open then table.insert( candidates, v ) end
end
if #candidates >= 1 then
table.sort( candidates, function (a,b) return a.rate > b.rate end )
return candidates[1].rate
else
return 0
end
end
map["DD"].open = false
map["JJ"].open = false
--print( releasedPressure())
--print(potentialRateFromAt("DD",1))
print("tests")
local actual = "AA"
local bestMove = { p = 0, steps = 0, dest = "" }
for steps = 1,(30) do
local t = idsAtSteps( actual, steps )
print("#",steps)
for _, id in ipairs(t) do
local valve = map[id]
local p = valve.open and 0 or valve.rate*(30-steps-1)
print(id, valve.rate, steps+1, (30-steps-1), valve.rate*(30-steps-1), valve.open, p)
end
end