diff --git a/2022/day05.py b/2022/day05.py new file mode 100644 index 0000000..28b26f9 --- /dev/null +++ b/2022/day05.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import copy +from typing import TextIO + +arrangement = {} + +arr_actual = {} + +move_instr = [] + + +def rearrange(arr: dict, old: int, new: int, times: int) -> None: + for _ in range(times): + move = arr[old].pop() + arr[new].append(move) + + +def rearrange_new(arr: dict, old: int, new: int, num: int) -> None: + move = arr[old][len(arr[old]) - num :] + + arr[old] = arr[old][: len(arr[old]) - num] + arr[new] = arr[new] + move + + +def part1(f: TextIO) -> str: + top = "" + + line = f.readline().rstrip() + + while line.split()[0].isdigit() != True: + for x in range(1, len(line), 4): + stack = (x + 3) // 4 + c = line[x] + + if c.isalpha(): + if stack not in arrangement: + arrangement[stack] = [c] + else: + arrangement[stack].insert(0, c) + + line = f.readline() + + global arr_actual + arr_actual = copy.deepcopy(arrangement) + + f.readline() + line = f.readline() + + while line != "": + line = line.split(" ") + + # for part 2 + move_instr.append(line) + + rearrange(arrangement, int(line[3]), int(line[5]), int(line[1])) + line = f.readline().strip() + + for i in sorted(arrangement.keys()): + top += arrangement[i][-1] + + return top + + +def part2() -> str: + top = "" + + for it in move_instr: + rearrange_new(arr_actual, int(it[3]), int(it[5]), int(it[1])) + + for i in sorted(arr_actual.keys()): + top += arr_actual[i][-1] + + return top + + +if __name__ == "__main__": + print(part1(open("day05.txt"))) + print(part2()) diff --git a/2022/day05.txt b/2022/day05.txt new file mode 100644 index 0000000..6e96408 --- /dev/null +++ b/2022/day05.txt @@ -0,0 +1,512 @@ + [M] [Z] [V] + [Z] [P] [L] [Z] [J] +[S] [D] [W] [W] [H] [Q] +[P] [V] [N] [D] [P] [C] [V] +[H] [B] [J] [V] [B] [M] [N] [P] +[V] [F] [L] [Z] [C] [S] [P] [S] [G] +[F] [J] [M] [G] [R] [R] [H] [R] [L] +[G] [G] [G] [N] [V] [V] [T] [Q] [F] + 1 2 3 4 5 6 7 8 9 + +move 6 from 9 to 3 +move 2 from 2 to 1 +move 1 from 8 to 2 +move 3 from 7 to 2 +move 7 from 6 to 9 +move 1 from 9 to 5 +move 3 from 5 to 7 +move 6 from 8 to 6 +move 1 from 7 to 8 +move 6 from 6 to 5 +move 4 from 5 to 8 +move 9 from 2 to 9 +move 1 from 2 to 3 +move 3 from 1 to 3 +move 3 from 5 to 1 +move 10 from 3 to 5 +move 4 from 4 to 6 +move 2 from 7 to 6 +move 2 from 6 to 9 +move 6 from 8 to 6 +move 1 from 4 to 3 +move 1 from 4 to 5 +move 1 from 4 to 1 +move 2 from 3 to 1 +move 1 from 3 to 7 +move 8 from 1 to 9 +move 1 from 1 to 2 +move 1 from 2 to 7 +move 6 from 6 to 3 +move 7 from 3 to 5 +move 14 from 5 to 6 +move 2 from 1 to 3 +move 5 from 5 to 8 +move 5 from 8 to 1 +move 2 from 7 to 1 +move 5 from 6 to 9 +move 8 from 9 to 3 +move 13 from 9 to 3 +move 7 from 1 to 4 +move 6 from 4 to 1 +move 22 from 3 to 1 +move 1 from 9 to 3 +move 2 from 6 to 1 +move 1 from 3 to 4 +move 7 from 9 to 8 +move 2 from 1 to 7 +move 2 from 3 to 2 +move 2 from 6 to 9 +move 2 from 7 to 8 +move 1 from 3 to 6 +move 9 from 8 to 6 +move 1 from 2 to 4 +move 8 from 1 to 2 +move 1 from 9 to 4 +move 3 from 4 to 1 +move 1 from 4 to 6 +move 10 from 6 to 5 +move 5 from 2 to 9 +move 6 from 9 to 3 +move 2 from 5 to 3 +move 2 from 9 to 7 +move 7 from 5 to 8 +move 5 from 6 to 2 +move 3 from 3 to 7 +move 3 from 3 to 5 +move 4 from 5 to 8 +move 1 from 3 to 5 +move 6 from 6 to 8 +move 1 from 5 to 7 +move 9 from 8 to 9 +move 1 from 3 to 1 +move 7 from 2 to 7 +move 9 from 7 to 6 +move 2 from 2 to 3 +move 7 from 9 to 3 +move 9 from 6 to 8 +move 7 from 3 to 4 +move 2 from 7 to 6 +move 4 from 4 to 5 +move 3 from 5 to 6 +move 2 from 7 to 4 +move 5 from 4 to 7 +move 13 from 8 to 4 +move 2 from 9 to 4 +move 2 from 8 to 7 +move 6 from 7 to 5 +move 6 from 4 to 2 +move 1 from 7 to 5 +move 3 from 2 to 7 +move 1 from 7 to 8 +move 3 from 2 to 4 +move 2 from 3 to 9 +move 2 from 7 to 2 +move 6 from 5 to 4 +move 3 from 6 to 2 +move 2 from 6 to 9 +move 5 from 2 to 9 +move 12 from 4 to 8 +move 3 from 9 to 2 +move 12 from 1 to 5 +move 4 from 4 to 6 +move 12 from 8 to 9 +move 2 from 6 to 5 +move 1 from 4 to 8 +move 1 from 4 to 1 +move 3 from 2 to 1 +move 2 from 6 to 7 +move 1 from 5 to 9 +move 2 from 1 to 4 +move 10 from 5 to 1 +move 2 from 7 to 3 +move 18 from 9 to 7 +move 8 from 7 to 2 +move 1 from 9 to 6 +move 1 from 6 to 7 +move 10 from 7 to 9 +move 1 from 4 to 2 +move 19 from 1 to 5 +move 8 from 5 to 9 +move 3 from 8 to 4 +move 2 from 5 to 2 +move 2 from 3 to 6 +move 10 from 5 to 2 +move 4 from 1 to 2 +move 2 from 9 to 2 +move 1 from 1 to 6 +move 2 from 5 to 6 +move 1 from 8 to 7 +move 1 from 5 to 8 +move 1 from 5 to 6 +move 18 from 2 to 5 +move 5 from 2 to 1 +move 6 from 5 to 8 +move 1 from 8 to 9 +move 2 from 2 to 4 +move 1 from 2 to 6 +move 2 from 7 to 6 +move 1 from 4 to 1 +move 4 from 8 to 5 +move 1 from 2 to 9 +move 2 from 8 to 3 +move 1 from 3 to 6 +move 1 from 4 to 8 +move 1 from 8 to 9 +move 10 from 5 to 7 +move 5 from 5 to 1 +move 2 from 4 to 1 +move 3 from 7 to 6 +move 12 from 1 to 4 +move 8 from 9 to 5 +move 6 from 7 to 4 +move 1 from 7 to 9 +move 4 from 4 to 3 +move 1 from 1 to 7 +move 3 from 9 to 5 +move 2 from 3 to 1 +move 1 from 7 to 6 +move 8 from 4 to 7 +move 1 from 7 to 6 +move 7 from 6 to 4 +move 2 from 1 to 3 +move 1 from 7 to 1 +move 1 from 3 to 7 +move 1 from 1 to 6 +move 4 from 9 to 3 +move 5 from 4 to 6 +move 12 from 6 to 2 +move 3 from 9 to 4 +move 8 from 2 to 6 +move 2 from 9 to 6 +move 8 from 5 to 6 +move 4 from 5 to 8 +move 14 from 6 to 3 +move 11 from 4 to 9 +move 2 from 2 to 7 +move 8 from 3 to 9 +move 11 from 3 to 6 +move 14 from 9 to 1 +move 7 from 1 to 3 +move 2 from 9 to 5 +move 2 from 2 to 8 +move 6 from 7 to 5 +move 1 from 9 to 8 +move 13 from 6 to 3 +move 4 from 6 to 8 +move 3 from 1 to 6 +move 5 from 5 to 8 +move 7 from 8 to 7 +move 2 from 1 to 8 +move 1 from 4 to 1 +move 4 from 8 to 9 +move 8 from 7 to 5 +move 1 from 8 to 1 +move 4 from 9 to 3 +move 1 from 4 to 5 +move 5 from 5 to 2 +move 1 from 8 to 9 +move 1 from 8 to 6 +move 2 from 6 to 2 +move 4 from 8 to 6 +move 4 from 1 to 8 +move 4 from 8 to 5 +move 1 from 9 to 8 +move 1 from 2 to 3 +move 4 from 6 to 1 +move 1 from 8 to 2 +move 3 from 5 to 4 +move 4 from 2 to 5 +move 1 from 7 to 9 +move 1 from 2 to 6 +move 3 from 1 to 8 +move 2 from 4 to 5 +move 2 from 6 to 1 +move 3 from 8 to 9 +move 4 from 9 to 2 +move 1 from 7 to 1 +move 1 from 6 to 7 +move 4 from 1 to 6 +move 1 from 7 to 4 +move 6 from 2 to 8 +move 2 from 4 to 8 +move 1 from 9 to 5 +move 3 from 6 to 2 +move 1 from 6 to 4 +move 7 from 3 to 5 +move 2 from 8 to 1 +move 3 from 2 to 8 +move 6 from 8 to 5 +move 17 from 5 to 3 +move 2 from 1 to 6 +move 3 from 8 to 3 +move 1 from 9 to 5 +move 11 from 5 to 2 +move 40 from 3 to 5 +move 11 from 2 to 7 +move 4 from 7 to 8 +move 1 from 8 to 9 +move 1 from 3 to 5 +move 1 from 4 to 8 +move 19 from 5 to 8 +move 7 from 7 to 8 +move 16 from 5 to 2 +move 6 from 5 to 8 +move 1 from 5 to 8 +move 1 from 9 to 4 +move 1 from 6 to 1 +move 1 from 4 to 7 +move 1 from 6 to 9 +move 1 from 1 to 7 +move 1 from 7 to 3 +move 1 from 7 to 2 +move 1 from 9 to 8 +move 1 from 3 to 4 +move 1 from 4 to 6 +move 14 from 2 to 9 +move 24 from 8 to 4 +move 8 from 8 to 3 +move 1 from 6 to 3 +move 16 from 4 to 1 +move 3 from 8 to 4 +move 3 from 3 to 8 +move 4 from 3 to 4 +move 1 from 3 to 9 +move 13 from 9 to 4 +move 16 from 1 to 8 +move 8 from 8 to 1 +move 3 from 1 to 7 +move 1 from 8 to 6 +move 1 from 3 to 8 +move 10 from 8 to 5 +move 5 from 5 to 2 +move 3 from 8 to 9 +move 1 from 8 to 9 +move 1 from 4 to 5 +move 5 from 2 to 6 +move 3 from 5 to 2 +move 1 from 6 to 1 +move 5 from 1 to 5 +move 1 from 1 to 5 +move 2 from 7 to 3 +move 2 from 3 to 2 +move 1 from 5 to 7 +move 7 from 5 to 3 +move 5 from 9 to 5 +move 2 from 7 to 9 +move 4 from 5 to 6 +move 2 from 9 to 8 +move 2 from 2 to 4 +move 5 from 3 to 5 +move 1 from 3 to 2 +move 7 from 4 to 9 +move 1 from 8 to 1 +move 1 from 2 to 1 +move 9 from 4 to 6 +move 2 from 1 to 8 +move 1 from 3 to 9 +move 2 from 8 to 6 +move 13 from 4 to 6 +move 1 from 8 to 7 +move 2 from 9 to 6 +move 3 from 5 to 7 +move 3 from 2 to 5 +move 3 from 2 to 6 +move 5 from 6 to 2 +move 4 from 2 to 5 +move 4 from 5 to 7 +move 5 from 5 to 7 +move 7 from 9 to 6 +move 6 from 7 to 2 +move 22 from 6 to 5 +move 10 from 5 to 8 +move 7 from 5 to 4 +move 8 from 8 to 5 +move 18 from 6 to 2 +move 5 from 7 to 5 +move 1 from 8 to 2 +move 6 from 5 to 1 +move 7 from 4 to 2 +move 4 from 1 to 5 +move 1 from 7 to 9 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 10 from 5 to 9 +move 12 from 2 to 1 +move 8 from 5 to 2 +move 19 from 2 to 9 +move 1 from 6 to 8 +move 13 from 9 to 3 +move 8 from 1 to 2 +move 5 from 1 to 3 +move 10 from 2 to 1 +move 7 from 2 to 5 +move 3 from 5 to 7 +move 4 from 1 to 3 +move 1 from 2 to 3 +move 3 from 1 to 2 +move 1 from 8 to 6 +move 2 from 7 to 5 +move 4 from 1 to 3 +move 6 from 5 to 4 +move 2 from 2 to 1 +move 1 from 2 to 9 +move 6 from 4 to 5 +move 5 from 5 to 9 +move 1 from 6 to 8 +move 1 from 5 to 1 +move 6 from 9 to 2 +move 5 from 2 to 4 +move 3 from 1 to 6 +move 2 from 4 to 7 +move 22 from 3 to 9 +move 1 from 8 to 4 +move 2 from 4 to 3 +move 2 from 6 to 1 +move 2 from 1 to 5 +move 1 from 6 to 7 +move 1 from 7 to 4 +move 6 from 3 to 7 +move 1 from 2 to 4 +move 8 from 7 to 3 +move 1 from 4 to 5 +move 1 from 7 to 9 +move 5 from 3 to 6 +move 1 from 8 to 4 +move 4 from 3 to 2 +move 32 from 9 to 3 +move 3 from 6 to 7 +move 5 from 9 to 3 +move 1 from 9 to 7 +move 2 from 9 to 2 +move 2 from 4 to 3 +move 2 from 5 to 4 +move 5 from 3 to 2 +move 3 from 7 to 8 +move 1 from 7 to 2 +move 1 from 8 to 5 +move 1 from 3 to 4 +move 5 from 4 to 5 +move 4 from 5 to 2 +move 3 from 5 to 7 +move 1 from 7 to 5 +move 1 from 6 to 5 +move 2 from 8 to 5 +move 15 from 2 to 4 +move 3 from 5 to 6 +move 4 from 6 to 5 +move 2 from 5 to 2 +move 1 from 2 to 4 +move 25 from 3 to 9 +move 2 from 5 to 2 +move 11 from 9 to 2 +move 13 from 2 to 1 +move 4 from 4 to 7 +move 12 from 9 to 8 +move 6 from 7 to 8 +move 7 from 4 to 7 +move 7 from 7 to 8 +move 1 from 5 to 1 +move 5 from 4 to 3 +move 2 from 2 to 1 +move 2 from 9 to 5 +move 7 from 1 to 7 +move 1 from 1 to 4 +move 12 from 3 to 2 +move 1 from 3 to 9 +move 1 from 1 to 3 +move 1 from 9 to 1 +move 7 from 7 to 2 +move 1 from 4 to 7 +move 2 from 8 to 7 +move 7 from 1 to 2 +move 1 from 3 to 4 +move 26 from 2 to 1 +move 4 from 8 to 1 +move 3 from 1 to 6 +move 1 from 6 to 3 +move 1 from 6 to 9 +move 1 from 3 to 8 +move 20 from 1 to 3 +move 1 from 9 to 7 +move 4 from 7 to 1 +move 1 from 5 to 3 +move 4 from 3 to 5 +move 1 from 6 to 2 +move 6 from 3 to 2 +move 8 from 1 to 4 +move 1 from 1 to 5 +move 3 from 1 to 4 +move 7 from 2 to 4 +move 10 from 3 to 8 +move 4 from 4 to 3 +move 12 from 4 to 7 +move 3 from 3 to 1 +move 2 from 4 to 3 +move 2 from 8 to 1 +move 6 from 8 to 9 +move 5 from 9 to 6 +move 1 from 9 to 3 +move 3 from 8 to 7 +move 10 from 8 to 5 +move 4 from 8 to 7 +move 9 from 7 to 9 +move 4 from 8 to 4 +move 2 from 4 to 3 +move 3 from 1 to 7 +move 11 from 7 to 4 +move 6 from 4 to 8 +move 1 from 7 to 3 +move 4 from 5 to 1 +move 5 from 3 to 6 +move 5 from 9 to 4 +move 1 from 9 to 8 +move 10 from 4 to 8 +move 5 from 1 to 2 +move 1 from 7 to 6 +move 9 from 6 to 3 +move 7 from 8 to 7 +move 3 from 4 to 1 +move 2 from 2 to 1 +move 9 from 8 to 3 +move 10 from 5 to 8 +move 18 from 3 to 9 +move 1 from 7 to 8 +move 1 from 5 to 3 +move 4 from 8 to 3 +move 2 from 6 to 3 +move 6 from 7 to 2 +move 1 from 5 to 3 +move 1 from 1 to 9 +move 10 from 3 to 9 +move 4 from 1 to 8 +move 13 from 8 to 1 +move 3 from 1 to 8 +move 3 from 2 to 4 +move 5 from 2 to 6 +move 5 from 6 to 4 +move 28 from 9 to 2 +move 2 from 9 to 5 +move 2 from 5 to 2 +move 1 from 3 to 7 +move 2 from 1 to 4 +move 3 from 8 to 3 +move 1 from 9 to 4 +move 3 from 4 to 6 +move 2 from 3 to 7 +move 8 from 1 to 5 +move 3 from 7 to 6 +move 14 from 2 to 8 +move 1 from 9 to 1 +move 6 from 5 to 6 +move 4 from 2 to 5 +move 9 from 8 to 2 +move 4 from 8 to 4 +move 7 from 2 to 4 +move 12 from 4 to 3 +move 5 from 4 to 7 +move 5 from 7 to 4 +move 1 from 8 to 7 +move 1 from 4 to 5 +move 2 from 5 to 4 +move 1 from 5 to 8 +move 1 from 5 to 9 diff --git a/2022/day05_test.py b/2022/day05_test.py new file mode 100644 index 0000000..553fa18 --- /dev/null +++ b/2022/day05_test.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from day05 import * + + +def test_part1() -> None: + f = open("day05.txt") + assert part1(f) == "FCVRLMVQP" + f.close() + + +def test_part2() -> None: + assert part2() == "RWLWGJGFD"