Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.4 kB
3
Indexable
Never
def day_five():
    stacks = []
    result = ""
    result2 = ""
    with open('input5.txt') as f:
        txt = f.readlines()
        i = txt.index(" 1   2   3   4   5   6   7   8   9 \n")
        numb = txt[i]
        numb = numb.replace(" ", "").rstrip("\n")
        createnumb = 1
        for x in numb:
            stacks.append(x)
        length = len(stacks)
        for create in txt[i - 1::-1]:
            for i in range(length):
                if not create[createnumb] == " ":
                    stacks[i] += create[createnumb]
                createnumb += 4
            createnumb = 1
        list2 = stacks[:]
        for move in txt[i + 2::]:
            move = move.replace("move ", "").replace(" from ", ",").replace(" to ", ",").rstrip("\n")
            move = move.split(",")
            creatsNum = int(move[0]) + 1
            fromStack = int(move[1]) - 1
            toStack = int(move[2]) - 1

            for x in stacks[fromStack][:-creatsNum:-1]:
                stacks[toStack] += x

            stacks[fromStack] = stacks[fromStack][:-(creatsNum - 1)]
            list2[toStack] += list2[fromStack][-creatsNum + 1:]
            list2[fromStack] = list2[fromStack][:-(creatsNum - 1)]

    for x in stacks:
        result += x[-1]
    for x in list2:
        result2 += x[-1]
    return f"Day 5: part 1- {result} part 2 - {result2}"