adventofcode-2021/day10/part2.lua

44 lines
610 B
Lua
Executable File

#!/usr/bin/env luajit
require("deque")
require("utils")
opening = {
["["] = "]",
["{"] = "}",
["<"] = ">",
["("] = ")",
}
score = {
["]"] = 2,
["}"] = 3,
[">"] = 4,
[")"] = 1,
}
local scores = {}
for line in io.lines(arg[3]) do
d = deque()
for c in chars(line) do
if opening[c] then
d:append(opening[c])
else
if c ~= d:pop() then
d = deque()
break
end
end
end
local linescore = 0
while d.size > 0 do
linescore = linescore * 5 + score[d:pop()]
end
if linescore ~= 0 then
table.insert(scores, linescore)
end
end
table.sort(scores)
print(scores[(#scores + 1) / 2])