-
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 #188 from Stegallo/2015
2015
- Loading branch information
Showing
5 changed files
with
122 additions
and
2 deletions.
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.day6 import Day | ||
|
||
with patch("builtins.open", mock_open(read_data="command 0,0 through 999,999")): | ||
day = Day() | ||
|
||
|
||
def test_calculate_1(): | ||
day._input_data = [["turn on 0,0 through 9,0", "turn off 8,0 through 9,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_1() == 8 | ||
|
||
day._input_data = [["turn off 0,0 through 9,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_1() == 0 | ||
|
||
day._input_data = [["toggle 0,0 through 9,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_1() == 10 | ||
|
||
|
||
def test_calculate_2(): | ||
day._input_data = [["turn on 0,0 through 0,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_2() == 1 | ||
|
||
day._input_data = [["turn off 0,0 through 0,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_2() == 0 | ||
|
||
day._input_data = [["toggle 0,0 through 0,0"]] | ||
day._preprocess_input() | ||
assert day._calculate_2() == 2 |
File renamed without changes.
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from common.aoc import AoCDay | ||
|
||
|
||
@dataclass() | ||
@dataclass | ||
class Position: | ||
x: int | ||
y: int | ||
|
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,84 @@ | ||
import re | ||
from typing import Dict | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
from common.aoc import AoCDay | ||
|
||
|
||
@dataclass | ||
class Instruction: | ||
command: str | ||
start_x: int | ||
start_y: int | ||
end_x: int | ||
end_y: int | ||
|
||
def process(self, grid, actions=None) -> None: | ||
min_x = min(self.start_x, self.end_x) | ||
min_y = min(self.start_y, self.end_y) | ||
max_x = max(self.start_x, self.end_x) | ||
max_y = max(self.start_y, self.end_y) | ||
|
||
for i in range(min_x, max_x + 1): | ||
for j in range(min_y, max_y + 1): | ||
hash = Light.hash_fun(i, j) | ||
if hash not in grid: | ||
grid[hash] = Light(i, j) | ||
light = grid[hash] | ||
light.brightness = actions[self.command](light.brightness) | ||
|
||
|
||
@dataclass | ||
class Light: | ||
x: int | ||
y: int | ||
brightness: int = 0 | ||
|
||
@staticmethod | ||
def hash_fun(x: int, y: int): | ||
return f"x={x};y={y}" | ||
|
||
@property | ||
def hash(self) -> str: | ||
return Light.hash_fun(self.x, self.y) | ||
|
||
|
||
class Day(AoCDay): | ||
def __init__(self, test=0): | ||
super().__init__(__name__, test) | ||
|
||
def _preprocess_input(self): | ||
self.__input_data = [ | ||
Instruction( | ||
*list(re.search(r"(\D*) (\d*),(\d*) through (\d*),(\d*)", i).groups()), | ||
) | ||
for i in self._input_data[0] | ||
] | ||
|
||
def _calculate_1(self) -> int: | ||
grid: Dict[str, Light] = {} | ||
|
||
actions = { | ||
"turn on": lambda x: True, | ||
"turn off": lambda x: False, | ||
"toggle": lambda x: not x, | ||
} | ||
for i in self.__input_data: | ||
i.process(grid, actions) | ||
|
||
return sum(i.brightness for i in grid.values()) | ||
|
||
def _calculate_2(self) -> int: | ||
grid: Dict[str, Light] = {} | ||
|
||
actions = { | ||
"turn on": lambda x: x + 1, | ||
"turn off": lambda x: max(x - 1, 0), | ||
"toggle": lambda x: x + 2, | ||
} | ||
|
||
for i in self.__input_data: | ||
i.process(grid, actions) | ||
|
||
return sum(i.brightness for i in grid.values()) |