From 354dac8de751be2989ac3be1cddbfabb4bc18ae8 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Mon, 5 Dec 2022 17:57:08 +0100 Subject: [PATCH] day 5: we do a little bit of numpy --- day05.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/day05.py b/day05.py index 1a4f8e5..4e9c3fe 100644 --- a/day05.py +++ b/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))