From 69932539a7860f84edd8ffca58a33b239a47a7b7 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 10 Dec 2022 19:24:03 +0100 Subject: [PATCH] Solve day 10 part 1 --- 2022/day10_cathode.py | 57 ++++++++++++++++ 2022/inputs/day10 | 145 +++++++++++++++++++++++++++++++++++++++ 2022/inputs/day10-test1 | 146 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 21 ------ requirements.txt | 4 ++ 5 files changed, 352 insertions(+), 21 deletions(-) create mode 100644 2022/day10_cathode.py create mode 100644 2022/inputs/day10 create mode 100644 2022/inputs/day10-test1 create mode 100644 requirements.txt diff --git a/2022/day10_cathode.py b/2022/day10_cathode.py new file mode 100644 index 0000000..592e2ae --- /dev/null +++ b/2022/day10_cathode.py @@ -0,0 +1,57 @@ +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + data = f.read().strip().split("\n") + + data = parse_data(data) + solution_part_1 = solve_part_1(data) + + print(f"1. Found {solution_part_1}") + if expected_part_1: + assert expected_part_1 == solution_part_1 + + solution_part_2 = solve_part_2(data) + print(f"2. Found {solution_part_2}") + if expected_part_2: + assert expected_part_2 == solution_part_2 + + +DataType = list[str] + + +def parse_data(data: list[str]) -> DataType: + return data + + +def solve_part_1(data: DataType) -> int: + total = 0 + register = 1 + clock = 0 + expected_cycles = [20, 60, 100, 140, 180, 220] + for instruction in data: + match instruction.split(): + case ["noop"]: + clock += 1 + if clock in expected_cycles: + print(f"{clock=}, {register=}") + total += register * clock + case ["addx", value]: + clock += 1 + if clock in expected_cycles: + print(f"{clock=}, {register=}") + total += register * clock + clock += 1 + if clock in expected_cycles: + print(f"{clock=}, {register=}") + total += register * clock + register += int(value) + return total + + +def solve_part_2(data: DataType) -> int: + return 0 + + +if __name__ == "__main__": + main("inputs/day10-test1", expected_part_1=13140) + main("inputs/day10") diff --git a/2022/inputs/day10 b/2022/inputs/day10 new file mode 100644 index 0000000..6402c1a --- /dev/null +++ b/2022/inputs/day10 @@ -0,0 +1,145 @@ +noop +noop +noop +addx 3 +addx 7 +noop +noop +noop +noop +addx 6 +noop +addx -1 +noop +addx 5 +addx 1 +noop +addx 4 +noop +noop +noop +noop +addx 6 +addx -1 +noop +addx 3 +addx -13 +addx -22 +noop +noop +addx 3 +addx 2 +addx 11 +addx -4 +addx 11 +addx -10 +addx 2 +addx 5 +addx 2 +addx -2 +noop +addx 7 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -2 +addx -8 +addx -27 +addx 5 +addx 2 +addx 21 +addx -21 +addx 3 +addx 5 +addx 2 +addx -3 +addx 4 +addx 3 +addx 1 +addx 5 +noop +noop +noop +noop +addx 3 +addx 1 +addx 6 +addx -31 +noop +addx -4 +noop +noop +noop +noop +addx 3 +addx 7 +noop +addx -1 +addx 1 +addx 5 +noop +addx 1 +noop +addx 2 +addx -8 +addx 15 +addx 3 +noop +addx 2 +addx 5 +noop +noop +noop +addx -28 +addx 11 +addx -20 +noop +addx 7 +addx -2 +addx 7 +noop +addx -2 +noop +addx -6 +addx 11 +noop +addx 3 +addx 2 +noop +noop +addx 7 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -16 +addx -10 +addx -11 +addx 27 +addx -20 +noop +addx 2 +addx 3 +addx 5 +noop +noop +noop +addx 3 +addx -2 +addx 2 +noop +addx -14 +addx 21 +noop +addx -6 +addx 12 +noop +addx -21 +addx 24 +addx 2 +noop +noop +noop diff --git a/2022/inputs/day10-test1 b/2022/inputs/day10-test1 new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/inputs/day10-test1 @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/pyproject.toml b/pyproject.toml index f6a6d0d..28b4479 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,24 +1,3 @@ -[tool.poetry] -name = "advent-of-code" -version = "0.1.0" -description = "" -authors = ["Gabriel Augendre "] -license = "Unlicense" - -[tool.poetry.dependencies] -python = "^3.10" -networkx = ">=2.5" -pdbpp = ">=0.10.2" - -[tool.poetry.dev-dependencies] -pre-commit = ">=2.9.2" -pytest = ">=6.1.2" - -[build-system] -requires = ["poetry-core>=1.0.0"] -build-backend = "poetry.core.masonry.api" - - ############################################################################### # flake8 / flakeheaven ############################################################################### diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f1da2b7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +networkx>=2.5 + +pre-commit +pytest