-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from Stegallo/2015
2015
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |