day 5: we do a little bit of numpy
This commit is contained in:
parent
612b61a1ac
commit
354dac8de7
1 changed files with 18 additions and 19 deletions
37
day05.py
37
day05.py
|
@ -1,27 +1,26 @@
|
|||
import fileinput
|
||||
import sys
|
||||
from collections import defaultdict, deque
|
||||
import re
|
||||
from copy import deepcopy
|
||||
import numpy
|
||||
|
||||
input = sys.stdin.read().split("\n")
|
||||
inp_stacks, inp_moves = (x.split("\n") for x in sys.stdin.read().split("\n\n", 1))
|
||||
|
||||
rows = (line[1::4] for line in input[::-1] if "[" in line)
|
||||
stacks = [[c for c in x if c != ' '] for x in zip(*rows)]
|
||||
stacks2 = deepcopy(stacks)
|
||||
|
||||
RE = re.compile(r"move (\d+) from (\d+) to (\d+)")
|
||||
for line in input:
|
||||
m = RE.match(line)
|
||||
if not m:
|
||||
rows = (line[1::4] for line in inp_stacks[::-1] if "[" in line)
|
||||
stacks = [numpy.array([ord(c) for c in x if c != ' '], dtype=numpy.int8) for x in zip(*rows)]
|
||||
stacks1 = numpy.array(stacks)
|
||||
stacks2 = numpy.ndarray.copy(stacks1)
|
||||
|
||||
for i, line in enumerate(inp_moves):
|
||||
if i % 10000 == 0:
|
||||
print(i, len(inp_moves), end="\r")
|
||||
if not line:
|
||||
continue
|
||||
|
||||
n, f, t = int(m[1]), int(m[2]) - 1, int(m[3]) - 1
|
||||
_, a, _, b, _, c = line.split()
|
||||
n, f, t = int(a), int(b) - 1, int(c) - 1
|
||||
|
||||
stacks[t].extend(reversed(stacks[f][-n:]))
|
||||
del stacks[f][-n:]
|
||||
stacks1[t] = numpy.concatenate((stacks1[t], stacks1[f][-n:][::-1]))
|
||||
stacks1[f] = stacks1[f][:-n]
|
||||
|
||||
stacks2[t].extend(stacks2[f][-n:])
|
||||
del stacks2[f][-n:]
|
||||
stacks2[t] = numpy.concatenate((stacks2[t], stacks2[f][-n:]))
|
||||
stacks2[f] = stacks2[f][:-n]
|
||||
|
||||
print("".join(s[-1] for s in stacks), "".join(s[-1] for s in stacks2))
|
||||
print("".join(chr(s[-1]) for s in stacks1), "".join(chr(s[-1]) for s in stacks2))
|
||||
|
|
Loading…
Reference in a new issue