Roderic Day il y a 3 ans
Parent
révision
29e5079b85
1 fichiers modifiés avec 23 ajouts et 0 suppressions
  1. +23
    -0
      y2021/p10.py

+ 23
- 0
y2021/p10.py Voir le fichier

@@ -0,0 +1,23 @@
pts = {')': 3, ']': 57, '}': 1197, '>': 25137}
objs = '()[]{}<>'
fam = lambda c: objs.index(c) // 2
val = lambda c: objs.index(c) % 2
ans1 = 0
scores = []
for ln in text.splitlines():
stack = []
for c in ln:
if val(c) == 0:
stack.append(c)
elif fam(c) == fam(stack[-1]) and val(c) == 1:
stack.pop()
else:
ans1 += pts[c]
break
else:
score = 0
while stack:
score *= 5
score += fam(stack.pop()) + 1
scores.append(score)
ans2 = sorted(scores)[len(scores) // 2]

Chargement…
Annuler
Enregistrer