Skip to content

Commit

Permalink
Merge pull request #171 from Stegallo/2015
Browse files Browse the repository at this point in the history
2015
  • Loading branch information
Stegallo authored Nov 4, 2023
2 parents 1132c62 + b23c42e commit bdf6601
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
| | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 |
| - | - | - | - | - | - | - | - | - |
| 01 | [puzzle][201501p]</br>[][201501] | | | | | | | |
| 02 | [puzzle][201502p]</br>[][201501] | | | | | | | |
| 02 | [puzzle][201502p]</br>[][201502] | | | | | | | |
| 03 | [puzzle][201503p]</br>[][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

Expand Down
36 changes: 36 additions & 0 deletions tests/y_2015/test_2015_day3.py
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions y_2015/day3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from pydantic.dataclasses import dataclass

from common.aoc import AoCDay


@dataclass()
class Position:
x: int
y: int

@property
def hash(self) -> str:
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
current_position = Position(0, 0)

def set_grid(self, grid) -> None:
self.grid = grid

def deliver_presents(self) -> None:
for i in self.sequence:
self.current_position = Position(
self.current_position.x + OPERATIONS[i].x,
self.current_position.y + OPERATIONS[i].y,
)

self.grid.add(self.current_position.hash)


class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)

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])
)
self.__robo_santa = Santa(
"".join([i for c, i in enumerate(self._input_data[0][0]) if c % 2 != 0])
)

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) -> int:
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)

0 comments on commit bdf6601

Please sign in to comment.