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
|
import sys
|
||||||
from collections import defaultdict, deque
|
import numpy
|
||||||
import re
|
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
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)
|
rows = (line[1::4] for line in inp_stacks[::-1] if "[" in line)
|
||||||
stacks = [[c for c in x if c != ' '] for x in zip(*rows)]
|
stacks = [numpy.array([ord(c) for c in x if c != ' '], dtype=numpy.int8) for x in zip(*rows)]
|
||||||
stacks2 = deepcopy(stacks)
|
stacks1 = numpy.array(stacks)
|
||||||
|
stacks2 = numpy.ndarray.copy(stacks1)
|
||||||
RE = re.compile(r"move (\d+) from (\d+) to (\d+)")
|
|
||||||
for line in input:
|
for i, line in enumerate(inp_moves):
|
||||||
m = RE.match(line)
|
if i % 10000 == 0:
|
||||||
if not m:
|
print(i, len(inp_moves), end="\r")
|
||||||
|
if not line:
|
||||||
continue
|
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:]))
|
stacks1[t] = numpy.concatenate((stacks1[t], stacks1[f][-n:][::-1]))
|
||||||
del stacks[f][-n:]
|
stacks1[f] = stacks1[f][:-n]
|
||||||
|
|
||||||
stacks2[t].extend(stacks2[f][-n:])
|
stacks2[t] = numpy.concatenate((stacks2[t], stacks2[f][-n:]))
|
||||||
del 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