From 77dbf430448bada4a5643d996dd8e97198d97a93 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 3 Nov 2023 21:27:07 -0700 Subject: [PATCH 1/5] day3 --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dd8b876..1ea9843 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,15 @@ | | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | | - | - | - | - | - | - | - | - | - | | 01 | [puzzle][201501p]
[✓][201501] | | | | | | | | -| 02 | [puzzle][201502p]
[✓][201501] | | | | | | | | +| 02 | [puzzle][201502p]
[✓][201502] | | | | | | | | +| 03 | [puzzle][201503p]
[✓][201503] | | | | | | | | [201501]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day1.py [201501p]: https://adventofcode.com/2015/day/1 [201502]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day2.py [201502p]: https://adventofcode.com/2015/day/2 +[201503]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day3.py +[201503p]: https://adventofcode.com/2015/day/3 ## to run the code From a18e9151501bdd8b1e642ea2d9e43b8abb73bc46 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 4 Nov 2023 13:15:52 -0700 Subject: [PATCH 2/5] day3 --- tests/y_2015/test_2015_day3.py | 36 +++++++++++++++++ y_2015/day3.py | 73 ++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tests/y_2015/test_2015_day3.py create mode 100644 y_2015/day3.py diff --git a/tests/y_2015/test_2015_day3.py b/tests/y_2015/test_2015_day3.py new file mode 100644 index 0000000..2226562 --- /dev/null +++ b/tests/y_2015/test_2015_day3.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2015.day3 import Day + +with patch("builtins.open", mock_open(read_data="1x1x1")): + day = Day() + + +def test_calculate_1(): + day._input_data = [[">"]] + day._preprocess_input() + assert day._calculate_1() == 2 + + day._input_data = [["^>v<"]] + day._preprocess_input() + assert day._calculate_1() == 4 + + day._input_data = [["^v^v^v^v^v"]] + day._preprocess_input() + assert day._calculate_1() == 2 + + +def test_calculate_2(): + day._input_data = [["^v"]] + day._preprocess_input() + assert day._calculate_2() == 3 + + day._input_data = [["^>v<"]] + day._preprocess_input() + assert day._calculate_2() == 3 + + day._input_data = [["^v^v^v^v^v"]] + day._preprocess_input() + assert day._calculate_2() == 11 diff --git a/y_2015/day3.py b/y_2015/day3.py new file mode 100644 index 0000000..ac8597b --- /dev/null +++ b/y_2015/day3.py @@ -0,0 +1,73 @@ +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay + + +@dataclass() +class Position: + x: int + y: int + + @property + def hash(self): + return f"x={self.x};y={self.y}" + + +@dataclass +class Santa: + sequence: str + current_position = Position(0, 0) + + def set_grid(self, grid): + self.grid = grid + + def deliver_presents(self): + for i in self.sequence: + if i == ">": + self.current_position = Position( + self.current_position.x + 1, self.current_position.y + ) + if i == "<": + self.current_position = Position( + self.current_position.x - 1, self.current_position.y + ) + + if i == "^": + self.current_position = Position( + self.current_position.x, self.current_position.y + 1 + ) + if i == "v": + self.current_position = Position( + self.current_position.x, self.current_position.y - 1 + ) + + if self.current_position.hash not in self.grid: + self.grid.add(self.current_position.hash) + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__santa = Santa(self._input_data[0][0]) + self.__real_santa = Santa( + "".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 == 0]) + ) + self.__robo_santa = Santa( + "".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 != 0]) + ) + + def _calculate_1(self): + grid = {Position(0, 0).hash} + self.__santa.set_grid(grid) + self.__santa.deliver_presents() + return len(self._Day__santa.grid) + + def _calculate_2(self): + grid = {Position(0, 0).hash} + self.__real_santa.set_grid(grid) + self.__robo_santa.set_grid(grid) + self.__real_santa.deliver_presents() + self.__robo_santa.deliver_presents() + return len(grid) From b40fa1a317eb764d05288198868e0502d0916be3 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 4 Nov 2023 14:25:38 -0700 Subject: [PATCH 3/5] day3 --- y_2015/day3.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/y_2015/day3.py b/y_2015/day3.py index ac8597b..334d078 100644 --- a/y_2015/day3.py +++ b/y_2015/day3.py @@ -2,6 +2,8 @@ from common.aoc import AoCDay +OPERATIONS = {">": (1, 0), "<": (-1, 0), "^": (0, 1), "v": (0, -1)} + @dataclass() class Position: @@ -23,26 +25,13 @@ def set_grid(self, grid): def deliver_presents(self): for i in self.sequence: - if i == ">": - self.current_position = Position( - self.current_position.x + 1, self.current_position.y - ) - if i == "<": - self.current_position = Position( - self.current_position.x - 1, self.current_position.y - ) - - if i == "^": - self.current_position = Position( - self.current_position.x, self.current_position.y + 1 - ) - if i == "v": - self.current_position = Position( - self.current_position.x, self.current_position.y - 1 - ) + move = OPERATIONS[i] + self.current_position = Position( + self.current_position.x + move[0], + self.current_position.y + move[1], + ) - if self.current_position.hash not in self.grid: - self.grid.add(self.current_position.hash) + self.grid.add(self.current_position.hash) class Day(AoCDay): From 322cd039eb62c963076ac930ba9ea5f52dadbd4b Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 4 Nov 2023 14:28:47 -0700 Subject: [PATCH 4/5] day3 --- y_2015/day3.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/y_2015/day3.py b/y_2015/day3.py index 334d078..c45fd83 100644 --- a/y_2015/day3.py +++ b/y_2015/day3.py @@ -2,8 +2,6 @@ from common.aoc import AoCDay -OPERATIONS = {">": (1, 0), "<": (-1, 0), "^": (0, 1), "v": (0, -1)} - @dataclass() class Position: @@ -15,6 +13,14 @@ def hash(self): return f"x={self.x};y={self.y}" +OPERATIONS = { + ">": Position(1, 0), + "<": Position(-1, 0), + "^": Position(0, 1), + "v": Position(0, -1), +} + + @dataclass class Santa: sequence: str @@ -27,8 +33,8 @@ def deliver_presents(self): for i in self.sequence: move = OPERATIONS[i] self.current_position = Position( - self.current_position.x + move[0], - self.current_position.y + move[1], + self.current_position.x + move.x, + self.current_position.y + move.y, ) self.grid.add(self.current_position.hash) From b23c42e57215103b6b922dfa5425b80ee03d064b Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 4 Nov 2023 14:34:41 -0700 Subject: [PATCH 5/5] day3 --- y_2015/day3.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/y_2015/day3.py b/y_2015/day3.py index c45fd83..9b67e56 100644 --- a/y_2015/day3.py +++ b/y_2015/day3.py @@ -9,7 +9,7 @@ class Position: y: int @property - def hash(self): + def hash(self) -> str: return f"x={self.x};y={self.y}" @@ -26,15 +26,14 @@ class Santa: sequence: str current_position = Position(0, 0) - def set_grid(self, grid): + def set_grid(self, grid) -> None: self.grid = grid - def deliver_presents(self): + def deliver_presents(self) -> None: for i in self.sequence: - move = OPERATIONS[i] self.current_position = Position( - self.current_position.x + move.x, - self.current_position.y + move.y, + self.current_position.x + OPERATIONS[i].x, + self.current_position.y + OPERATIONS[i].y, ) self.grid.add(self.current_position.hash) @@ -44,7 +43,7 @@ class Day(AoCDay): def __init__(self, test=0): super().__init__(__name__, test) - def _preprocess_input(self): + def _preprocess_input(self) -> None: self.__santa = Santa(self._input_data[0][0]) self.__real_santa = Santa( "".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 == 0]) @@ -53,13 +52,13 @@ def _preprocess_input(self): "".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 != 0]) ) - def _calculate_1(self): + def _calculate_1(self) -> int: grid = {Position(0, 0).hash} self.__santa.set_grid(grid) self.__santa.deliver_presents() return len(self._Day__santa.grid) - def _calculate_2(self): + def _calculate_2(self) -> int: grid = {Position(0, 0).hash} self.__real_santa.set_grid(grid) self.__robo_santa.set_grid(grid)