Skip to content

Commit

Permalink
day5 test works
Browse files Browse the repository at this point in the history
  • Loading branch information
mf-tdr committed Dec 6, 2022
1 parent 28bb4d7 commit 9649e06
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
49 changes: 49 additions & 0 deletions day5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import re
import numpy as np


def parse_file(fn):
stack = []
instructions = []
with open(fn, "r") as f:
for line in f.readlines():
if "[" in line:
stack.append([line[i+1] for i in range(0, len(line)-1, 4)])

with open(fn, "r") as f:
search = r"move (\d*) from (\d*) to (\d*)"
instructions = re.findall(search, f.read())
instructions = [[int(el) for el in inst] for inst in instructions]

return stack, instructions


if __name__ == "__main__":
stack, instructions = parse_file("day5_test1.txt")

stack = np.array(stack)
print(stack)

empty_line = np.array([[' ']*stack.shape[0]])
print(empty_line)

for number, st_from, st_to in instructions:
print(number, st_from, st_to)
temp = []
for _ in range(number):
index, el = next(((i, el) for (i, el) in enumerate(stack[:, st_from-1]) if el != ' '))
print(el)
temp.append(el)
stack[index, st_from-1] = ' '

for el in temp:
index = next((i for i, el in enumerate(stack[:, st_to-1]) if el != ' '), None)
if index == 0:
stack = np.concatenate((empty_line, stack), axis=0)
index = 1
elif index is None:
index = stack.shape[1]+1
print(index)
stack[index-1, st_to-1] = el

print(stack)
9 changes: 9 additions & 0 deletions day5_test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

0 comments on commit 9649e06

Please sign in to comment.