summaryrefslogtreecommitdiffstats
path: root/5.py
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-07 12:57:36 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-07 12:57:36 +0000
commitd9a97e9bf28cf91f1a7e57f1298b7c742e25ed56 (patch)
tree999464f801480565733c5223b020b2d762140b00 /5.py
downloadaoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.tar.gz
aoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.tar.xz
aoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.zip
days 1-7
Diffstat (limited to '5.py')
-rw-r--r--5.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/5.py b/5.py
new file mode 100644
index 0000000..d375f74
--- /dev/null
+++ b/5.py
@@ -0,0 +1,29 @@
+from copy import deepcopy
+with open('5.in.test') as f:
+ stacks, instructions = f.read().rstrip().split('\n\n')
+layers = stacks.split('\n')
+stacks = [list() for _ in range((len(layers[-1]) + 1) // 4)]
+for layer in reversed(layers[:-1]):
+ for i in range(len(stacks)):
+ c = layer[i * 4 + 1]
+ if c != ' ':
+ stacks[i].append(c)
+instructions = [i.split(' ') for i in instructions.split('\n')]
+instructions = [tuple(int(i) for i in (i[1], i[3], i[5])) for i in instructions]
+def part1(stacks, instructions):
+ stacks = deepcopy(stacks)
+ for count, src, dst in instructions:
+ for _ in range(count):
+ stacks[dst-1].append(stacks[src-1].pop())
+ return ''.join(s[-1] for s in stacks)
+def part2(stacks, instructions):
+ stacks = deepcopy(stacks)
+ for count, src, dst in instructions:
+ tmp = list()
+ for _ in range(count):
+ tmp.append(stacks[src-1].pop())
+ for _ in range(count):
+ stacks[dst-1].append(tmp.pop())
+ return ''.join(s[-1] for s in stacks)
+print(part1(stacks, instructions))
+print(part2(stacks, instructions))