Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.

import math
file = open("input.txt")
stack = [[],[],[],[],[],[],[],[],[]]
stack2 =  [[],[],[],[],[],[],[],[],[]]
lines = file.readlines()
stacks = lines[0:8]
cmds = lines[10:]
for i in range(0,8):
    line = stacks[7-i]
    for j in range(1, 37, 4):
        if j > 33:
            break;
        if line[j].isalpha():
            stack[math.ceil(j/4)-1].append(line[j])
            stack2[math.ceil(j/4)-1].append(line[j])
for line in cmds:
    line = line.rstrip().split()
    c_f_t = [line[1], line[3], line[5]]
    for i in range(0, int(c_f_t[0])):
        stack[int(c_f_t[2])-1].append(stack[int(c_f_t[1])-1].pop())
    l = len(stack2[int(c_f_t[1])-1])-1
    to_append = stack2[int(c_f_t[1])-1][l-i:]
    for i in to_append:
        stack2[int(c_f_t[2])-1].append(i)
    stack2[int(c_f_t[1])-1]= stack2[int(c_f_t[1])-1][:-int(c_f_t[0])]
print(stack)
print(stack2)

I'll definitely have to clean this one up, I got flustered and started band aiding everything together because i had to get back to work

Jump in the discussion.

No email address required.

As an aside, I feel like part 2 and part 1 should have been reversed.

Why start with stacks and then enhance it by going back to arrays?

Jump in the discussion.

No email address required.

Instead of cleaning it up, have this technically-only-12-lines (((compact))) version instead

import math, copy
stack = [[],[],[],[],[],[],[],[],[]]
lines = open("input.txt").readlines()
stacks = lines[0:8]
cmds = lines[10:]
[stack[math.ceil(j/4)-1].append(stacks[7-i][j]) for i in range(0,8) for j in range(1, 37, 4) if stacks[7-i][j].isalpha() ]
stack2 = copy.deepcopy(stack)
for line in cmds:
    for i in range(0, int(line.rstrip().split()[1])): stack[int(line.rstrip().split()[5])-1].append(stack[int(line.rstrip().split()[3])-1].pop())       
    for i in stack2[int(line.rstrip().split()[3])-1][len(stack2[int(line.rstrip().split()[3])-1])-1-i:]: stack2[int(line.rstrip().split()[5])-1].append(i)     
    stack2[int(line.rstrip().split()[3])-1]= stack2[int(line.rstrip().split()[3])-1][:-int(line.rstrip().split()[1])]
print(stack, stack2)
Jump in the discussion.

No email address required.



Now playing: Title Theme (Diddy Kong Racing).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.