diff --git a/day10/part1.lua b/day10/part1.lua new file mode 100755 index 0000000..f476476 --- /dev/null +++ b/day10/part1.lua @@ -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) diff --git a/day10/part2.lua b/day10/part2.lua new file mode 100755 index 0000000..7f44035 --- /dev/null +++ b/day10/part2.lua @@ -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])