advent-of-code/2021/ten.nim

32 lines
1,014 B
Nim
Raw Normal View History

2021-12-10 01:34:19 -06:00
import ./common, std/[strutils, sequtils, algorithm, sugar, tables]
2021-12-09 23:25:20 -06:00
2021-12-10 01:34:19 -06:00
const pairs = toTable {'(': ')', '[': ']', '{': '}', '<': '>'}
const corruptPoints = toTable {')': 3, ']': 57, '}': 1197, '>': 25137}
2021-12-09 23:25:20 -06:00
2021-12-10 01:34:19 -06:00
proc parse(input: seq[string]): (int, int) =
var cmps = newSeq[int]()
for line in input:
2021-12-09 23:25:20 -06:00
var stack: seq[char] = @[]
2021-12-10 01:34:19 -06:00
for ch in line:
if pairs.hasKey ch: stack.add pairs[ch]
elif stack.pop != ch:
result[0] += corruptPoints[ch]
stack.setLen 0
break
if stack.len > 0: cmps.add stack.mapIt(" )]}>".find it).foldr 5 * b + a
result[1] = cmps.sorted[cmps.len div 2]
2021-12-09 23:25:20 -06:00
2021-12-10 01:34:19 -06:00
doDay 10, n => n.loadInput(), s => s.parse[0], s => s.parse[1],
@[
"[({(<(())[]>[[{[]{<()<>>",
"[(()[<>])]({[<{<<[]>>(",
"{([(<{}[<>[]}>{[]{[(<()>",
"(((({<>}<{<{<>}{[]{[]{}",
"[[<[([]))<([[{}[[()]]]",
"[{[{({}]{}}([{[{{{}}([]",
"{<[[]]>}<{[{[{[]{()[[[]",
"[<(<(<(<{}))><([]([]()",
"<{([([[(<>()){}]>(<<{{",
"<{([{{}}[<[[[<>{}]]]>[]]",
], 26397, 288957