Skip to content

Commit

Permalink
Merge pull request #188 from Stegallo/2015
Browse files Browse the repository at this point in the history
2015
  • Loading branch information
Stegallo authored Nov 7, 2023
2 parents a8ccba0 + b5a3e4f commit 404adda
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from unittest.mock import mock_open, patch

from y_2022.day0 import Day
from y_1900.day0 import Day

with patch("builtins.open", mock_open(read_data="0")):
day = Day()
Expand Down
36 changes: 36 additions & 0 deletions tests/y_2015/test_2015_day6.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.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.
2 changes: 1 addition & 1 deletion y_2015/day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from common.aoc import AoCDay


@dataclass()
@dataclass
class Position:
x: int
y: int
Expand Down
84 changes: 84 additions & 0 deletions y_2015/day6.py
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())

0 comments on commit 404adda

Please sign in to comment.