From f315ef05c548da80410cb4f6665a9bba7a953f94 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Tue, 2 Dec 2025 07:57:38 +0100 Subject: Day 1 and 2 --- day1/__init__.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ day1/example.txt | 10 ++++++++++ day1/test_init.py | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 day1/__init__.py create mode 100644 day1/example.txt create mode 100644 day1/test_init.py (limited to 'day1') diff --git a/day1/__init__.py b/day1/__init__.py new file mode 100644 index 0000000..6e95329 --- /dev/null +++ b/day1/__init__.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +from abc import ABC +from typing import Iterator + +from aoc import BaseAssignment, I + + +class Assignment(BaseAssignment, ABC): + def parse_item(self, item: str) -> Iterator[I]: + [rotation, *places] = item + places = int("".join(places)) + + yield -places if rotation == "L" else places + + +class AssignmentOne(Assignment): + example_result = 3 + + def run(self, input: Iterator[int]) -> int: + seen_0 = 0 + position = 50 + for rotation in input: + position = (position + rotation) % 100 + if position == 0: + seen_0 += 1 + + return seen_0 + + +class AssignmentTwo(Assignment): + example_result = 6 + + @staticmethod + def calculate_new_position(position, rotation) -> tuple[int, int]: + previous_position = position + new_position = position + rotation + + overturned = new_position >= 100 or new_position < 0 + + multiplier = position - (0 - -previous_position) // 100 + + if overturned: + new_position = new_position % 100 + + seen_0 = 0 + if new_position == 0 and multiplier == 0: + seen_0 += multiplier + 1 + elif overturned: + seen_0 += multiplier + + return new_position, seen_0 + + def run(self, input: Iterator[int]) -> int: + seen_0_total = 0 + position = 50 + for rotation in input: + position, seen_0 = self.calculate_new_position(position, rotation) + seen_0_total += seen_0 + + return seen_0_total diff --git a/day1/example.txt b/day1/example.txt new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/day1/example.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/day1/test_init.py b/day1/test_init.py new file mode 100644 index 0000000..a24e272 --- /dev/null +++ b/day1/test_init.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +import pytest + +from day1 import AssignmentTwo + + +class TestAssignmentTwo: + data = [ + # Example Data + (50, -68, 82, 1), + (82, -30, 52, 0), + (52, 48, 0, 1), + (0, -5, 95, 0), + (95, 60, 55, 1), + (55, -55, 0, 1), + (0, -1, 99, 0), + (99, -99, 0, 1), + (0, 14, 14, 0), + (14, -82, 32, 1), + # Additional cases + (0, 10, 10, 0), + (0, -10, 90, 0), + (0, 100, 0, 1), + (0, -100, 0, 1), + (80, 40, 20, 1), + (80, 140, 20, 2), + (80, 100, 80, 1), + (0, -300, 0, 3), + (0, 299, 99, 2), + (0, -299, 1, 2), + ] + + @pytest.mark.parametrize("position,rotation,new_position,seen_0", data) + def test_calculate_new_position(self, position, rotation, new_position, seen_0): + assert AssignmentTwo.calculate_new_position(position, rotation) == ( + new_position, + seen_0, + ) -- cgit v1.2.3