Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 14:44 | 1:57 | 16:41 |
Pretty simple, just treating lists like stacks. I was slowed down by my bizarre decision to treat the front of a list as the top of the stack. .pop()
defaults to getting the last element in a list...
import aocd
import re
with open("sess") as f:
sess = f.readline()
din = aocd.get_data(session=sess, year=2022, day=5).split("\n")
size = 9
stacks = [[]] * size
stop = False
for line in din:
if line.strip() == "":
stop = True
print(stacks)
continue
if not stop:
a = 0
for i in range(1, size * 4 + 1, 4):
if line[i] != ' ' and line[i].isalpha():
stacks[a] = stacks[a] + [line[i]]
a += 1
else:
instr = list(map(int, re.findall(r'\d+', line)))
### SECTION TO BE MODIFIED FOR PART 2 ###
for i in range(0, instr[0]):
stacks[instr[2] - 1] = [stacks[instr[1] - 1].pop(0)] + stacks[instr[2] - 1]
### END SECTION ###
print(stacks)
for thing in stacks:
print(thing[0], end="")
Very small modification to prepend the all of the pops in order rather than prepending them one by one. This line replaces all code in the section marked above.
stacks[instr[2] - 1] = [stacks[instr[1] - 1].pop(0) for i in range(0, instr[0])] + stacks[instr[2] - 1]