This commit is contained in:
Felix Van der Jeugt 2021-12-10 08:54:13 +01:00
parent e058039f35
commit bcd54e369b
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 78 additions and 0 deletions

35
day10/part1.lua Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env luajit
require("deque")
require("utils")
opening = {
["["] = "]",
["{"] = "}",
["<"] = ">",
["("] = ")",
}
score = {
["]"] = 57,
["}"] = 1197,
[">"] = 25137,
[")"] = 3,
}
local sum = 0
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
sum = sum + score[c]
break
end
end
end
end
print(sum)

43
day10/part2.lua Executable file
View File

@ -0,0 +1,43 @@
#!/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])