day 10
This commit is contained in:
parent
e058039f35
commit
bcd54e369b
35
day10/part1.lua
Executable file
35
day10/part1.lua
Executable 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
43
day10/part2.lua
Executable 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])
|
Loading…
Reference in New Issue
Block a user