day ten done
This commit is contained in:
parent
6a63d729e0
commit
a5620c8197
50
2021/ten.nim
Normal file
50
2021/ten.nim
Normal file
|
@ -0,0 +1,50 @@
|
|||
import ./common, std/[strutils, sequtils, strformat, sugar, sets, math, algorithm, tables]
|
||||
|
||||
const p = {'(': ')', '[': ']', '{': '}', '<': '>'}.toTable()
|
||||
const t = {')': 3, ']': 57, '}': 1197, '>': 25137}.toTable()
|
||||
proc p1(s: seq[string]): int =
|
||||
var stack: seq[char] = @[]
|
||||
for l in s:
|
||||
for c in l:
|
||||
if p.hasKey(c): stack.add(p[c])
|
||||
else:
|
||||
if stack.pop() != c: result += t[c]
|
||||
echo result
|
||||
|
||||
const v = {')': 1, ']': 2, '}': 3, '>': 4}.toTable()
|
||||
proc p2(s: seq[string]): int =
|
||||
var scores: seq[int] = @[]
|
||||
for l in s:
|
||||
var score = 0
|
||||
var stack: seq[char] = @[]
|
||||
for c in l:
|
||||
if p.hasKey(c): stack.add(p[c])
|
||||
else:
|
||||
if stack.pop() != c:
|
||||
score = -1
|
||||
break
|
||||
if score == -1: continue
|
||||
echo stack
|
||||
for l in stack.reversed():
|
||||
score *= 5
|
||||
score += v[l]
|
||||
echo score
|
||||
scores.add(score)
|
||||
scores.sort()
|
||||
echo scores
|
||||
result = scores[(scores.len() div 2)]
|
||||
echo result
|
||||
|
||||
doDay(10, (n) => n.loadInput(),
|
||||
p1,
|
||||
p2,
|
||||
"""[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]""".split('\n'), 26397, 288957)
|
Loading…
Reference in a new issue