From a37a55a9d4bd7ada7ca797194d722fcd2a65b8c7 Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 5 Dec 2022 11:15:29 +0100 Subject: [PATCH] 2022 - Day 5 part 1 --- 2022/5/5.py | 56 +++++ 2022/5/input.txt | 511 +++++++++++++++++++++++++++++++++++++++ 2022/5/input_example.txt | 9 + 3 files changed, 576 insertions(+) create mode 100644 2022/5/5.py create mode 100644 2022/5/input.txt create mode 100644 2022/5/input_example.txt diff --git a/2022/5/5.py b/2022/5/5.py new file mode 100644 index 0000000..d5d470c --- /dev/null +++ b/2022/5/5.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 5 + +# 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: + # print(move) + num_move = move[0] + from_stack = stacks[move[1] - 1] + dest_stack = stacks[move[2] - 1] + for n in range(0, num_move): + popped = from_stack.pop() + print(f'move {popped} from {move[1]} to {move[2]}') + dest_stack.append(popped) + # dest_stack.append(from_stack.pop()) + +print(stacks) +result = ''.join([s[-1] for s in stacks]) +print(result) diff --git a/2022/5/input.txt b/2022/5/input.txt new file mode 100644 index 0000000..85131c9 --- /dev/null +++ b/2022/5/input.txt @@ -0,0 +1,511 @@ + [L] [M] [M] + [D] [R] [Z] [C] [L] + [C] [S] [T] [G] [V] [M] +[R] [L] [Q] [B] [B] [D] [F] +[H] [B] [G] [D] [Q] [Z] [T] [J] +[M] [J] [H] [M] [P] [S] [V] [L] [N] +[P] [C] [N] [T] [S] [F] [R] [G] [Q] +[Z] [P] [S] [F] [F] [T] [N] [P] [W] + 1 2 3 4 5 6 7 8 9 + +move 7 from 3 to 9 +move 5 from 8 to 9 +move 3 from 9 to 5 +move 6 from 9 to 2 +move 9 from 9 to 3 +move 3 from 7 to 3 +move 8 from 2 to 3 +move 9 from 3 to 1 +move 11 from 3 to 8 +move 5 from 6 to 9 +move 1 from 6 to 3 +move 1 from 2 to 7 +move 1 from 4 to 8 +move 1 from 3 to 9 +move 4 from 4 to 3 +move 6 from 8 to 3 +move 2 from 8 to 2 +move 4 from 9 to 3 +move 3 from 2 to 5 +move 2 from 5 to 4 +move 5 from 3 to 4 +move 11 from 1 to 4 +move 1 from 7 to 6 +move 1 from 3 to 5 +move 2 from 1 to 9 +move 1 from 1 to 4 +move 7 from 5 to 8 +move 21 from 4 to 6 +move 6 from 6 to 2 +move 6 from 8 to 9 +move 5 from 8 to 5 +move 2 from 2 to 7 +move 4 from 3 to 7 +move 1 from 2 to 6 +move 1 from 2 to 5 +move 2 from 2 to 7 +move 4 from 3 to 7 +move 1 from 4 to 6 +move 9 from 5 to 3 +move 7 from 3 to 4 +move 7 from 7 to 3 +move 7 from 4 to 1 +move 8 from 3 to 5 +move 1 from 3 to 5 +move 3 from 8 to 2 +move 2 from 2 to 9 +move 13 from 9 to 4 +move 5 from 5 to 3 +move 4 from 7 to 6 +move 1 from 7 to 4 +move 2 from 4 to 2 +move 3 from 3 to 4 +move 2 from 5 to 2 +move 6 from 1 to 7 +move 1 from 2 to 8 +move 1 from 3 to 8 +move 1 from 1 to 6 +move 1 from 3 to 4 +move 1 from 2 to 6 +move 24 from 6 to 1 +move 3 from 2 to 3 +move 3 from 3 to 5 +move 2 from 8 to 6 +move 2 from 5 to 4 +move 3 from 5 to 1 +move 7 from 4 to 8 +move 3 from 8 to 9 +move 2 from 9 to 5 +move 2 from 6 to 3 +move 1 from 9 to 8 +move 5 from 7 to 5 +move 2 from 3 to 1 +move 1 from 7 to 1 +move 7 from 4 to 7 +move 2 from 4 to 8 +move 6 from 8 to 6 +move 3 from 6 to 9 +move 10 from 5 to 1 +move 7 from 7 to 1 +move 1 from 4 to 9 +move 1 from 6 to 3 +move 2 from 9 to 7 +move 1 from 4 to 2 +move 1 from 9 to 5 +move 1 from 8 to 5 +move 39 from 1 to 8 +move 1 from 2 to 5 +move 2 from 6 to 9 +move 3 from 9 to 5 +move 3 from 1 to 6 +move 1 from 7 to 2 +move 1 from 3 to 2 +move 2 from 6 to 2 +move 3 from 2 to 3 +move 1 from 6 to 2 +move 1 from 1 to 8 +move 3 from 1 to 2 +move 3 from 2 to 4 +move 2 from 4 to 5 +move 2 from 3 to 8 +move 8 from 5 to 2 +move 8 from 8 to 2 +move 15 from 2 to 7 +move 1 from 1 to 5 +move 25 from 8 to 7 +move 2 from 2 to 4 +move 2 from 4 to 3 +move 1 from 8 to 4 +move 2 from 4 to 6 +move 1 from 2 to 1 +move 26 from 7 to 2 +move 15 from 2 to 1 +move 7 from 8 to 9 +move 10 from 1 to 6 +move 10 from 7 to 2 +move 1 from 8 to 1 +move 5 from 9 to 8 +move 1 from 8 to 9 +move 2 from 6 to 9 +move 3 from 7 to 1 +move 1 from 7 to 1 +move 5 from 9 to 2 +move 1 from 3 to 1 +move 9 from 6 to 3 +move 1 from 6 to 1 +move 4 from 2 to 4 +move 3 from 4 to 8 +move 1 from 4 to 1 +move 9 from 3 to 1 +move 1 from 7 to 6 +move 9 from 2 to 5 +move 14 from 1 to 6 +move 1 from 3 to 8 +move 5 from 2 to 6 +move 8 from 1 to 8 +move 6 from 6 to 8 +move 14 from 6 to 7 +move 1 from 1 to 7 +move 10 from 5 to 4 +move 11 from 8 to 5 +move 15 from 7 to 1 +move 4 from 5 to 6 +move 4 from 8 to 9 +move 6 from 5 to 3 +move 1 from 6 to 9 +move 1 from 1 to 6 +move 1 from 5 to 8 +move 2 from 6 to 2 +move 6 from 1 to 5 +move 1 from 5 to 8 +move 2 from 5 to 4 +move 9 from 2 to 9 +move 13 from 9 to 8 +move 1 from 2 to 1 +move 1 from 4 to 8 +move 3 from 3 to 1 +move 2 from 4 to 5 +move 2 from 1 to 5 +move 1 from 9 to 3 +move 17 from 8 to 1 +move 3 from 3 to 2 +move 4 from 5 to 1 +move 2 from 2 to 4 +move 1 from 6 to 1 +move 1 from 2 to 8 +move 4 from 4 to 6 +move 1 from 5 to 9 +move 5 from 6 to 8 +move 1 from 5 to 4 +move 1 from 5 to 6 +move 3 from 8 to 6 +move 8 from 4 to 5 +move 32 from 1 to 7 +move 11 from 7 to 6 +move 8 from 5 to 3 +move 3 from 8 to 7 +move 6 from 3 to 9 +move 4 from 3 to 8 +move 5 from 8 to 2 +move 1 from 8 to 5 +move 11 from 6 to 3 +move 1 from 5 to 2 +move 2 from 8 to 6 +move 12 from 7 to 8 +move 2 from 6 to 2 +move 2 from 6 to 4 +move 5 from 2 to 5 +move 8 from 7 to 2 +move 2 from 7 to 1 +move 2 from 7 to 6 +move 5 from 5 to 4 +move 5 from 4 to 7 +move 5 from 8 to 2 +move 2 from 9 to 7 +move 5 from 8 to 4 +move 2 from 7 to 3 +move 2 from 9 to 3 +move 3 from 7 to 9 +move 1 from 1 to 8 +move 2 from 6 to 1 +move 2 from 9 to 8 +move 1 from 7 to 8 +move 1 from 2 to 5 +move 1 from 7 to 9 +move 7 from 4 to 3 +move 3 from 3 to 6 +move 5 from 8 to 6 +move 3 from 9 to 5 +move 16 from 3 to 1 +move 2 from 9 to 1 +move 7 from 1 to 8 +move 1 from 1 to 2 +move 5 from 8 to 2 +move 12 from 1 to 4 +move 1 from 3 to 5 +move 1 from 2 to 9 +move 1 from 9 to 4 +move 4 from 6 to 5 +move 5 from 6 to 1 +move 1 from 6 to 5 +move 1 from 1 to 4 +move 1 from 4 to 7 +move 1 from 3 to 7 +move 9 from 4 to 6 +move 2 from 7 to 8 +move 1 from 3 to 4 +move 2 from 8 to 9 +move 4 from 8 to 4 +move 4 from 2 to 8 +move 2 from 9 to 7 +move 2 from 7 to 8 +move 10 from 2 to 4 +move 1 from 2 to 1 +move 5 from 4 to 7 +move 1 from 1 to 3 +move 3 from 8 to 7 +move 6 from 7 to 2 +move 3 from 2 to 7 +move 1 from 6 to 7 +move 5 from 5 to 8 +move 4 from 1 to 3 +move 4 from 3 to 1 +move 8 from 4 to 2 +move 1 from 3 to 2 +move 2 from 7 to 2 +move 2 from 6 to 3 +move 4 from 7 to 2 +move 4 from 5 to 7 +move 14 from 2 to 7 +move 3 from 2 to 1 +move 3 from 8 to 2 +move 1 from 5 to 7 +move 6 from 2 to 4 +move 2 from 2 to 7 +move 2 from 3 to 6 +move 6 from 8 to 2 +move 4 from 6 to 4 +move 2 from 6 to 9 +move 4 from 4 to 2 +move 2 from 4 to 8 +move 10 from 7 to 2 +move 18 from 2 to 6 +move 2 from 2 to 6 +move 2 from 9 to 2 +move 2 from 8 to 5 +move 1 from 2 to 9 +move 1 from 2 to 9 +move 1 from 5 to 7 +move 1 from 2 to 6 +move 2 from 9 to 2 +move 6 from 7 to 3 +move 7 from 6 to 8 +move 5 from 7 to 2 +move 1 from 7 to 4 +move 1 from 5 to 7 +move 4 from 8 to 7 +move 5 from 2 to 3 +move 1 from 7 to 5 +move 2 from 2 to 8 +move 9 from 4 to 3 +move 13 from 6 to 8 +move 10 from 3 to 1 +move 1 from 5 to 2 +move 3 from 6 to 8 +move 5 from 1 to 2 +move 1 from 1 to 8 +move 2 from 4 to 3 +move 17 from 8 to 6 +move 5 from 6 to 3 +move 3 from 1 to 2 +move 9 from 6 to 5 +move 2 from 6 to 8 +move 5 from 5 to 9 +move 3 from 9 to 8 +move 3 from 1 to 3 +move 3 from 7 to 5 +move 6 from 5 to 8 +move 7 from 2 to 4 +move 1 from 6 to 3 +move 1 from 1 to 5 +move 4 from 4 to 5 +move 2 from 2 to 9 +move 3 from 1 to 3 +move 4 from 5 to 8 +move 1 from 4 to 5 +move 6 from 8 to 7 +move 1 from 5 to 2 +move 4 from 9 to 2 +move 2 from 5 to 9 +move 2 from 1 to 8 +move 2 from 4 to 9 +move 6 from 7 to 5 +move 3 from 5 to 2 +move 3 from 2 to 5 +move 10 from 8 to 3 +move 2 from 8 to 5 +move 3 from 2 to 5 +move 6 from 5 to 1 +move 4 from 5 to 6 +move 1 from 7 to 5 +move 23 from 3 to 7 +move 2 from 5 to 9 +move 2 from 1 to 5 +move 2 from 6 to 3 +move 6 from 3 to 1 +move 1 from 1 to 7 +move 4 from 3 to 1 +move 1 from 8 to 5 +move 2 from 9 to 2 +move 3 from 3 to 8 +move 2 from 6 to 8 +move 12 from 1 to 3 +move 1 from 9 to 7 +move 3 from 5 to 9 +move 9 from 3 to 8 +move 1 from 1 to 7 +move 1 from 9 to 4 +move 3 from 3 to 6 +move 3 from 2 to 1 +move 3 from 8 to 6 +move 1 from 4 to 2 +move 1 from 2 to 9 +move 1 from 2 to 7 +move 20 from 7 to 5 +move 3 from 7 to 3 +move 3 from 1 to 3 +move 5 from 8 to 1 +move 5 from 1 to 5 +move 4 from 5 to 2 +move 3 from 2 to 6 +move 3 from 8 to 7 +move 1 from 2 to 6 +move 2 from 8 to 6 +move 2 from 7 to 5 +move 2 from 3 to 6 +move 12 from 5 to 1 +move 6 from 5 to 7 +move 12 from 6 to 8 +move 4 from 9 to 3 +move 4 from 5 to 8 +move 3 from 1 to 5 +move 4 from 7 to 4 +move 3 from 5 to 9 +move 7 from 1 to 6 +move 1 from 1 to 3 +move 6 from 7 to 6 +move 1 from 1 to 3 +move 10 from 3 to 6 +move 10 from 6 to 2 +move 2 from 9 to 5 +move 4 from 6 to 5 +move 9 from 6 to 1 +move 16 from 8 to 7 +move 3 from 8 to 7 +move 1 from 8 to 1 +move 7 from 2 to 1 +move 1 from 5 to 9 +move 1 from 6 to 1 +move 2 from 2 to 1 +move 3 from 1 to 4 +move 1 from 6 to 8 +move 7 from 4 to 1 +move 1 from 8 to 2 +move 22 from 1 to 8 +move 18 from 7 to 9 +move 6 from 5 to 2 +move 2 from 2 to 7 +move 2 from 1 to 5 +move 4 from 7 to 6 +move 1 from 5 to 6 +move 2 from 8 to 2 +move 3 from 2 to 6 +move 1 from 5 to 6 +move 15 from 9 to 6 +move 6 from 9 to 5 +move 1 from 9 to 8 +move 1 from 2 to 9 +move 5 from 5 to 9 +move 9 from 8 to 6 +move 3 from 2 to 7 +move 12 from 8 to 9 +move 1 from 7 to 5 +move 1 from 5 to 7 +move 3 from 7 to 1 +move 17 from 6 to 3 +move 1 from 2 to 6 +move 2 from 1 to 4 +move 16 from 6 to 4 +move 7 from 4 to 6 +move 1 from 5 to 7 +move 8 from 4 to 5 +move 9 from 9 to 8 +move 16 from 3 to 7 +move 1 from 1 to 5 +move 3 from 5 to 1 +move 5 from 6 to 2 +move 3 from 1 to 7 +move 3 from 6 to 7 +move 3 from 9 to 3 +move 5 from 8 to 5 +move 11 from 5 to 7 +move 2 from 3 to 7 +move 1 from 2 to 1 +move 1 from 3 to 6 +move 17 from 7 to 9 +move 1 from 3 to 2 +move 3 from 4 to 6 +move 1 from 1 to 2 +move 1 from 6 to 4 +move 14 from 7 to 6 +move 15 from 9 to 6 +move 4 from 8 to 7 +move 1 from 4 to 7 +move 7 from 9 to 5 +move 5 from 2 to 9 +move 7 from 5 to 1 +move 3 from 1 to 7 +move 29 from 6 to 4 +move 1 from 2 to 4 +move 18 from 4 to 2 +move 3 from 1 to 4 +move 1 from 1 to 7 +move 18 from 2 to 4 +move 3 from 6 to 5 +move 15 from 4 to 1 +move 1 from 5 to 1 +move 1 from 5 to 4 +move 9 from 4 to 1 +move 5 from 1 to 3 +move 9 from 1 to 5 +move 2 from 4 to 3 +move 5 from 5 to 6 +move 3 from 7 to 9 +move 7 from 7 to 5 +move 6 from 4 to 6 +move 2 from 3 to 7 +move 6 from 5 to 8 +move 2 from 8 to 4 +move 1 from 8 to 9 +move 9 from 6 to 2 +move 3 from 9 to 3 +move 1 from 2 to 1 +move 6 from 7 to 4 +move 2 from 2 to 8 +move 3 from 9 to 5 +move 5 from 4 to 8 +move 1 from 6 to 9 +move 1 from 3 to 1 +move 1 from 3 to 4 +move 1 from 6 to 5 +move 1 from 9 to 3 +move 10 from 8 to 7 +move 3 from 9 to 2 +move 7 from 2 to 4 +move 6 from 5 to 7 +move 4 from 5 to 8 +move 7 from 3 to 2 +move 3 from 7 to 1 +move 9 from 1 to 5 +move 5 from 7 to 9 +move 7 from 1 to 4 +move 11 from 4 to 2 +move 4 from 8 to 3 +move 5 from 4 to 7 +move 4 from 4 to 1 +move 1 from 3 to 6 +move 12 from 7 to 4 +move 2 from 1 to 8 +move 5 from 9 to 7 +move 7 from 5 to 6 +move 1 from 1 to 4 +move 1 from 9 to 8 +move 1 from 4 to 7 +move 1 from 8 to 9 +move 5 from 7 to 9 +move 2 from 7 to 5 +move 2 from 6 to 3 +move 5 from 2 to 7 +move 1 from 7 to 8 +move 1 from 1 to 6 +move 3 from 5 to 1 diff --git a/2022/5/input_example.txt b/2022/5/input_example.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/5/input_example.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2