Move into 2022 folder

This commit is contained in:
Mark Hoekveen
2024-11-11 09:05:01 +01:00
parent 9dd6a4f275
commit e0d955ec6f
28 changed files with 5 additions and 11 deletions
+54
View File
@@ -0,0 +1,54 @@
stacks = None
def initStacks(s):
global stacks
stacks = []
for i in range(s):
stacks.append([])
def reverseStacks():
global stacks
newStacks = []
for stack in stacks:
rstack = list(reversed(stack))
newStacks.append(rstack)
stacks = newStacks
def move(amount, source, dest):
# Get substack of items to move
substack = stacks[source-1][-amount:]
# Remove the items from source
stacks[source-1] = stacks[source-1][:-amount]
# Get destination stack and place items
dststack = stacks[dest-1]
stacks[dest-1] = dststack + substack
def printAnswer():
answer = ""
for stack in stacks:
answer += stack[-1]
print(answer)
with open("input.txt", "r") as f:
line = "."
while line != "":
line = f.readline().strip()
s = int((len(line)-3)/4 + 1)
if stacks is None:
initStacks(s)
for i in range(s):
char = line[1+i*4]
if char != " ":
stacks[i].append(char)
reverseStacks()
print(stacks)
for line in f.readlines():
data = line.strip().split(" ")
amount = int(data[1])
source = int(data[3])
dest = int(data[5])
move(amount, source, dest)
print(stacks)
printAnswer()