05: Solve in Python
This commit is contained in:
parent
943002f3b7
commit
f1edb14ad5
1 changed files with 29 additions and 0 deletions
29
05/solution.py
Executable file
29
05/solution.py
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
insr = re.compile(r"move (\d+) from (\d+) to (\d+)")
|
||||
|
||||
stacksA = defaultdict(bytearray)
|
||||
stacksB = defaultdict(bytearray)
|
||||
|
||||
for l in sys.stdin:
|
||||
if "[" in l:
|
||||
for i, c in enumerate(l[1::4], 1):
|
||||
if c != ' ':
|
||||
c = ord(c)
|
||||
stacksA[i].append(c)
|
||||
stacksB[i].append(c)
|
||||
|
||||
im = insr.match(l)
|
||||
if im:
|
||||
n, f, t = map(int, im.groups())
|
||||
stacksA[t] = bytearray(reversed(stacksA[f][:n])) + stacksA[t]
|
||||
stacksA[f] = stacksA[f][n:]
|
||||
stacksB[t] = stacksB[f][:n] + stacksB[t]
|
||||
stacksB[f] = stacksB[f][n:]
|
||||
|
||||
print(f"""Silver: {"".join(chr(stacksA[i][0]) for i in sorted(stacksA))}
|
||||
Gold: {"".join(chr(stacksB[i][0]) for i in sorted(stacksB))}""")
|
Loading…
Reference in a new issue