From f8c9bcea54d379799099d47b2934dae0826f10e7 Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 5 Dec 2022 11:25:49 +0100 Subject: [PATCH] 2022 - Day 5 part 2 --- 2022/5/5_2.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2022/5/5_2.py diff --git a/2022/5/5_2.py b/2022/5/5_2.py new file mode 100644 index 0000000..7f93d7f --- /dev/null +++ b/2022/5/5_2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 5 part 2 + +# file = 'input_example.txt' +file = 'input.txt' + +parsed_stack = [] +num_crates = 0 # num crates by line +moves = [] +with open(file, encoding="utf-8") as f: + while True: + line = f.readline() + if line[1] == '1': + break + num_crates = int(len(line)/4) + crates = [] + for crate in range(0, num_crates): + letter = line[1 + crate * 4] + crates.append(letter) + parsed_stack.append(crates) + line = f.readline() + for line in f.readlines(): + split = line.strip().split(' ') + moves.append((int(split[1]), int(split[3]), int(split[5]))) + +print(f'moves: {moves}') +print(f'num crates by line: {num_crates}') +# print(parsed_stack) +print(f'init stack height: {len(parsed_stack)}') +stacks = [] + +for s in range(0, num_crates): + stack = [] + for h in range(len(parsed_stack)-1, -1, -1): + if parsed_stack[h][s] == ' ': + break + stack.append(parsed_stack[h][s]) + stacks.append(stack) + +for s in stacks: + print(s) + +for move in moves: + num_move = move[0] + from_stack = stacks[move[1] - 1] + dest_stack = stacks[move[2] - 1] + dest_stack.extend(from_stack[-num_move:]) + del from_stack[-num_move:] + +print(stacks) +result = ''.join([s[-1] for s in stacks]) +print(result)