From 5ba6a2f7a6ac30f899ff5715b66746c191a2ce94 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 15:32:05 -0800 Subject: [PATCH 1/9] day6 --- .../test_1900_day0.py} | 2 +- tests/y_2015/test_2015_day6.py | 30 +++++++++++++++++++ {y_2022 => y_1900}/day0.py | 0 y_2015/day6.py | 22 ++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) rename tests/{y_2022/test_2022_day0.py => y_1900/test_1900_day0.py} (93%) create mode 100644 tests/y_2015/test_2015_day6.py rename {y_2022 => y_1900}/day0.py (100%) create mode 100644 y_2015/day6.py diff --git a/tests/y_2022/test_2022_day0.py b/tests/y_1900/test_1900_day0.py similarity index 93% rename from tests/y_2022/test_2022_day0.py rename to tests/y_1900/test_1900_day0.py index e0b4a2e..854ca02 100644 --- a/tests/y_2022/test_2022_day0.py +++ b/tests/y_1900/test_1900_day0.py @@ -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() diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py new file mode 100644 index 0000000..0eac8b2 --- /dev/null +++ b/tests/y_2015/test_2015_day6.py @@ -0,0 +1,30 @@ +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="1x1x1")): + day = Day() + + +def test_calculate_1(): + assert False + # day._input_data = [["2x3x4"]] + # day._preprocess_input() + # assert day._calculate_1() == 58 + # + # day._input_data = [["1x1x10"]] + # day._preprocess_input() + # assert day._calculate_1() == 43 + + +def test_calculate_2(): + assert False + # day._input_data = [["2x3x4"]] + # day._preprocess_input() + # assert day._calculate_2() == 34 + # + # day._input_data = [["1x1x10"]] + # day._preprocess_input() + # assert day._calculate_2() == 14 diff --git a/y_2022/day0.py b/y_1900/day0.py similarity index 100% rename from y_2022/day0.py rename to y_1900/day0.py diff --git a/y_2015/day6.py b/y_2015/day6.py new file mode 100644 index 0000000..0712dd6 --- /dev/null +++ b/y_2015/day6.py @@ -0,0 +1,22 @@ +from common.aoc import AoCDay + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0][0] + + def _calculate_1(self): + return 0 + # sum(OPERATIONS[x] for x in self.__input_data) + + def _calculate_2(self): + return 0 + # result = 0 + # for j, x in enumerate(self.__input_data, start=1): + # result += OPERATIONS[x] + # if result == -1: + # return j + # From 1730f7a928fb22c49e42d00ee8018d78ece2a72d Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 19:58:30 -0800 Subject: [PATCH 2/9] day6 --- y_2015/day3.py | 2 +- y_2015/day6.py | 88 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/y_2015/day3.py b/y_2015/day3.py index 763613f..f6c869a 100644 --- a/y_2015/day3.py +++ b/y_2015/day3.py @@ -5,7 +5,7 @@ from common.aoc import AoCDay -@dataclass() +@dataclass class Position: x: int y: int diff --git a/y_2015/day6.py b/y_2015/day6.py index 0712dd6..78012ea 100644 --- a/y_2015/day6.py +++ b/y_2015/day6.py @@ -1,22 +1,92 @@ +import re + +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, part): + 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): + light = Light(i, j) + if light.hash not in grid: + grid[light.hash] = light + else: + light = grid[light.hash] + if part == 1: + light.act_1(self.command) + if not light.on: + del grid[light.hash] + if part == 2: + light.act_2(self.command) + + +@dataclass +class Light: + x: int + y: int + on: bool = False + brightness: int = 0 + + @property + def hash(self) -> str: + return f"x={self.x};y={self.y}" + + def act_1(self, command: str) -> None: + if command == "turn on": + self.on = True + if command == "turn off": + self.on = False + if command == "toggle": + self.on = not self.on + + def act_2(self, command: str) -> None: + if command == "turn on": + self.brightness += 1 + if command == "turn off": + self.brightness = max(self.brightness - 1, 0) + if command == "toggle": + self.brightness += 2 + + class Day(AoCDay): def __init__(self, test=0): super().__init__(__name__, test) def _preprocess_input(self): - self.__input_data = self._input_data[0][0] + 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): return 0 - # sum(OPERATIONS[x] for x in self.__input_data) + grid = {} + + for i in self.__input_data: + i.process(grid, 1) + + return len(grid.keys()) def _calculate_2(self): - return 0 - # result = 0 - # for j, x in enumerate(self.__input_data, start=1): - # result += OPERATIONS[x] - # if result == -1: - # return j - # + grid = {} + + for i in self.__input_data: + i.process(grid, 2) + + return sum(i.brightness for i in grid.values()) From 64bfc0f17981089b76b8985db8ce72a0c9bd30b0 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 20:06:18 -0800 Subject: [PATCH 3/9] day6 --- tests/y_2015/test_2015_day6.py | 24 +++++++----------------- y_2015/day6.py | 1 - 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py index 0eac8b2..7dd6549 100644 --- a/tests/y_2015/test_2015_day6.py +++ b/tests/y_2015/test_2015_day6.py @@ -4,27 +4,17 @@ from y_2015.day6 import Day -with patch("builtins.open", mock_open(read_data="1x1x1")): +with patch("builtins.open", mock_open(read_data="command 0,0 through 999,999")): day = Day() def test_calculate_1(): - assert False - # day._input_data = [["2x3x4"]] - # day._preprocess_input() - # assert day._calculate_1() == 58 - # - # day._input_data = [["1x1x10"]] - # day._preprocess_input() - # assert day._calculate_1() == 43 + day._input_data = [["turn on 0,0 through 999,0"]] + day._preprocess_input() + assert day._calculate_1() == 1000 def test_calculate_2(): - assert False - # day._input_data = [["2x3x4"]] - # day._preprocess_input() - # assert day._calculate_2() == 34 - # - # day._input_data = [["1x1x10"]] - # day._preprocess_input() - # assert day._calculate_2() == 14 + day._input_data = [["turn on 0,0 through 0,0"]] + day._preprocess_input() + assert day._calculate_2() == 1 diff --git a/y_2015/day6.py b/y_2015/day6.py index 78012ea..2301b8d 100644 --- a/y_2015/day6.py +++ b/y_2015/day6.py @@ -75,7 +75,6 @@ def _preprocess_input(self): ] def _calculate_1(self): - return 0 grid = {} for i in self.__input_data: From db4279ac2a95373edf87e00db74d72b20c6517eb Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 20:29:39 -0800 Subject: [PATCH 4/9] day6 --- tests/y_2015/test_2015_day6.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py index 7dd6549..486af1b 100644 --- a/tests/y_2015/test_2015_day6.py +++ b/tests/y_2015/test_2015_day6.py @@ -9,12 +9,28 @@ def test_calculate_1(): - day._input_data = [["turn on 0,0 through 999,0"]] + day._input_data = [["turn on 0,0 through 9,0"]] day._preprocess_input() - assert day._calculate_1() == 1000 + assert day._calculate_1() == 10 + + 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 From 911057501f9975a94b11d3601fc2b7acea498279 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 20:35:34 -0800 Subject: [PATCH 5/9] day6 --- tests/y_2015/test_2015_day6.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py index 486af1b..a3ce083 100644 --- a/tests/y_2015/test_2015_day6.py +++ b/tests/y_2015/test_2015_day6.py @@ -17,9 +17,9 @@ def test_calculate_1(): day._preprocess_input() assert day._calculate_1() == 0 - day._input_data = [["toggle 0,0 through 9,0"]] + day._input_data = [["toggle 0,0 through 99,0"]] day._preprocess_input() - assert day._calculate_1() == 10 + assert day._calculate_1() == 100 def test_calculate_2(): From 3c591cc4a829e64f707e90ce47b32764c12e8662 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 20:38:22 -0800 Subject: [PATCH 6/9] day6 --- tests/y_2015/test_2015_day6.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/y_2015/test_2015_day6.py b/tests/y_2015/test_2015_day6.py index a3ce083..ed76058 100644 --- a/tests/y_2015/test_2015_day6.py +++ b/tests/y_2015/test_2015_day6.py @@ -9,17 +9,17 @@ def test_calculate_1(): - day._input_data = [["turn on 0,0 through 9,0"]] + day._input_data = [["turn on 0,0 through 9,0", "turn off 8,0 through 9,0"]] day._preprocess_input() - assert day._calculate_1() == 10 + 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 99,0"]] + day._input_data = [["toggle 0,0 through 9,0"]] day._preprocess_input() - assert day._calculate_1() == 100 + assert day._calculate_1() == 10 def test_calculate_2(): From 08b941b07bc0eca9cfd9e17433ddab28889ebb5f Mon Sep 17 00:00:00 2001 From: Stegallo Date: Mon, 6 Nov 2023 21:09:49 -0800 Subject: [PATCH 7/9] day6 --- y_2015/day6.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/y_2015/day6.py b/y_2015/day6.py index 2301b8d..8db0335 100644 --- a/y_2015/day6.py +++ b/y_2015/day6.py @@ -21,15 +21,10 @@ def process(self, grid, part): for i in range(min_x, max_x + 1): for j in range(min_y, max_y + 1): - light = Light(i, j) - if light.hash not in grid: - grid[light.hash] = light - else: - light = grid[light.hash] + hash = Light.hash_fun(i, j) + light = grid[hash] if part == 1: light.act_1(self.command) - if not light.on: - del grid[light.hash] if part == 2: light.act_2(self.command) @@ -41,9 +36,13 @@ class Light: on: bool = False brightness: int = 0 + @staticmethod + def hash_fun(x: int, y: int): + return f"x={x};y={y}" + @property def hash(self) -> str: - return f"x={self.x};y={self.y}" + return Light.hash_fun(self.x, self.y) def act_1(self, command: str) -> None: if command == "turn on": @@ -80,7 +79,7 @@ def _calculate_1(self): for i in self.__input_data: i.process(grid, 1) - return len(grid.keys()) + return sum(i.on for i in grid.values()) def _calculate_2(self): grid = {} From da011078a339e68c764fe44c02c550009a90e1c3 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Tue, 7 Nov 2023 10:46:42 -0800 Subject: [PATCH 8/9] day6 --- y_2015/day6.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/y_2015/day6.py b/y_2015/day6.py index 8db0335..8984c43 100644 --- a/y_2015/day6.py +++ b/y_2015/day6.py @@ -1,4 +1,5 @@ import re +from typing import Dict from pydantic.dataclasses import dataclass @@ -22,6 +23,8 @@ def process(self, grid, part): 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] if part == 1: light.act_1(self.command) @@ -73,16 +76,16 @@ def _preprocess_input(self): for i in self._input_data[0] ] - def _calculate_1(self): - grid = {} + def _calculate_1(self) -> int: + grid: Dict[str, Light] = {} for i in self.__input_data: i.process(grid, 1) return sum(i.on for i in grid.values()) - def _calculate_2(self): - grid = {} + def _calculate_2(self) -> int: + grid: Dict[str, Light] = {} for i in self.__input_data: i.process(grid, 2) From b5a3e4f0977cf1b42fd7acc16fe1a4314867d5dc Mon Sep 17 00:00:00 2001 From: Stegallo Date: Tue, 7 Nov 2023 10:54:03 -0800 Subject: [PATCH 9/9] day6 --- y_2015/day6.py | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/y_2015/day6.py b/y_2015/day6.py index 8984c43..02573d0 100644 --- a/y_2015/day6.py +++ b/y_2015/day6.py @@ -14,7 +14,7 @@ class Instruction: end_x: int end_y: int - def process(self, grid, part): + 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) @@ -26,17 +26,13 @@ def process(self, grid, part): if hash not in grid: grid[hash] = Light(i, j) light = grid[hash] - if part == 1: - light.act_1(self.command) - if part == 2: - light.act_2(self.command) + light.brightness = actions[self.command](light.brightness) @dataclass class Light: x: int y: int - on: bool = False brightness: int = 0 @staticmethod @@ -47,22 +43,6 @@ def hash_fun(x: int, y: int): def hash(self) -> str: return Light.hash_fun(self.x, self.y) - def act_1(self, command: str) -> None: - if command == "turn on": - self.on = True - if command == "turn off": - self.on = False - if command == "toggle": - self.on = not self.on - - def act_2(self, command: str) -> None: - if command == "turn on": - self.brightness += 1 - if command == "turn off": - self.brightness = max(self.brightness - 1, 0) - if command == "toggle": - self.brightness += 2 - class Day(AoCDay): def __init__(self, test=0): @@ -79,15 +59,26 @@ def _preprocess_input(self): 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, 1) + i.process(grid, actions) - return sum(i.on for i in grid.values()) + 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, 2) + i.process(grid, actions) return sum(i.brightness for i in grid.values())