From 6d869783f7920201e0b20381a2b8bee0d8065982 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 11:17:36 -0800 Subject: [PATCH 1/5] day8 --- y_2024/day8.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 y_2024/day8.py diff --git a/y_2024/day8.py b/y_2024/day8.py new file mode 100644 index 0000000..1da5703 --- /dev/null +++ b/y_2024/day8.py @@ -0,0 +1,81 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass +from collections import defaultdict +from common.aoc import AoCDay + + +@dataclass +class Grid: + grid: dict[tuple[int, int], str] + height: int + length: int + values: Optional[dict[str, list]] = None + + def __post_init__(self) -> None: + self.values = defaultdict(list) + for i, k in self.grid.items(): + self.values[k].append(i) + + @staticmethod + def from_input(input_data): + input = [[i for i in chunk] for chunk in input_data] + + grid = {} + for y in input: + for c, x in enumerate(y): + for i, k in enumerate(x): + grid[(c, i)] = k + + return Grid(grid, c + 1, i + 1) + + def display(self) -> None: + for i in range(self.height): + line = [] + for j in range(self.length): + line.append(self.grid[(i, j)]) + print("".join(line)) + + def items(self): + return self.grid.items() + + def keys(self): + return self.grid.keys() + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.grid = Grid.from_input(self._input_data) + self.grid.display() + + def _anti2(self, a, b, i, include) -> list: + x = [] + start = 0 if include else 1 + for i in range(start, i): + x.append((a[0] - i * (b[0] - a[0]), a[1] - i * (b[1] - a[1]))) + x.append((b[0] + i * (b[0] - a[0]), b[1] + i * (b[1] - a[1]))) + return x + + def _common(self, i, include): + antin = {} + antennas = {k: v for k, v in self.grid.values.items() if k != "."} + for j in antennas.values(): + for x in range(len(j)): + for y in range(len(j)): + if j[x] == j[y]: + continue + ants = self._anti2(j[x], j[y], i, include) + for m in ants: + antin[m] = "O" + + result = sum(bool(antin.get(i)) for i in self.grid.keys()) + return result + + def _calculate_1(self): + return self._common(2, False) + + def _calculate_2(self): + return self._common(100, True) From 22c94c0274f4d279e793251cb816d05b3ad56ccf Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 11:19:25 -0800 Subject: [PATCH 2/5] common grid --- common/grid.py | 42 ++++++++++++++++++++++++++++++++++++++++++ y_2024/day8.py | 43 +------------------------------------------ 2 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 common/grid.py diff --git a/common/grid.py b/common/grid.py new file mode 100644 index 0000000..03e7135 --- /dev/null +++ b/common/grid.py @@ -0,0 +1,42 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass +from collections import defaultdict + + +@dataclass +class Grid: + grid: dict[tuple[int, int], str] + height: int + length: int + values: Optional[dict[str, list]] = None + + def __post_init__(self) -> None: + self.values = defaultdict(list) + for i, k in self.grid.items(): + self.values[k].append(i) + + @staticmethod + def from_input(input_data): + input = [[i for i in chunk] for chunk in input_data] + + grid = {} + for y in input: + for c, x in enumerate(y): + for i, k in enumerate(x): + grid[(c, i)] = k + + return Grid(grid, c + 1, i + 1) + + def display(self) -> None: + for i in range(self.height): + line = [] + for j in range(self.length): + line.append(self.grid[(i, j)]) + print("".join(line)) + + def items(self): + return self.grid.items() + + def keys(self): + return self.grid.keys() diff --git a/y_2024/day8.py b/y_2024/day8.py index 1da5703..e0095e4 100644 --- a/y_2024/day8.py +++ b/y_2024/day8.py @@ -1,46 +1,5 @@ -from typing import Optional - -from pydantic.dataclasses import dataclass -from collections import defaultdict from common.aoc import AoCDay - - -@dataclass -class Grid: - grid: dict[tuple[int, int], str] - height: int - length: int - values: Optional[dict[str, list]] = None - - def __post_init__(self) -> None: - self.values = defaultdict(list) - for i, k in self.grid.items(): - self.values[k].append(i) - - @staticmethod - def from_input(input_data): - input = [[i for i in chunk] for chunk in input_data] - - grid = {} - for y in input: - for c, x in enumerate(y): - for i, k in enumerate(x): - grid[(c, i)] = k - - return Grid(grid, c + 1, i + 1) - - def display(self) -> None: - for i in range(self.height): - line = [] - for j in range(self.length): - line.append(self.grid[(i, j)]) - print("".join(line)) - - def items(self): - return self.grid.items() - - def keys(self): - return self.grid.keys() +from common.grid import Grid class Day(AoCDay): From bc55dbd4d123a6eecf23316cec505d0492f5b291 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 12:31:46 -0800 Subject: [PATCH 3/5] Update y_2024/day8.py Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- y_2024/day8.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/y_2024/day8.py b/y_2024/day8.py index 1da5703..32ed22b 100644 --- a/y_2024/day8.py +++ b/y_2024/day8.py @@ -55,8 +55,12 @@ def _anti2(self, a, b, i, include) -> list: x = [] start = 0 if include else 1 for i in range(start, i): - x.append((a[0] - i * (b[0] - a[0]), a[1] - i * (b[1] - a[1]))) - x.append((b[0] + i * (b[0] - a[0]), b[1] + i * (b[1] - a[1]))) + x.extend( + ( + (a[0] - i * (b[0] - a[0]), a[1] - i * (b[1] - a[1])), + (b[0] + i * (b[0] - a[0]), b[1] + i * (b[1] - a[1])), + ) + ) return x def _common(self, i, include): From fc667f1991de19ce196b7c006893c0cc4f8ecf5c Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 12:32:00 -0800 Subject: [PATCH 4/5] Update y_2024/day8.py Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- y_2024/day8.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/y_2024/day8.py b/y_2024/day8.py index 32ed22b..c267364 100644 --- a/y_2024/day8.py +++ b/y_2024/day8.py @@ -75,8 +75,7 @@ def _common(self, i, include): for m in ants: antin[m] = "O" - result = sum(bool(antin.get(i)) for i in self.grid.keys()) - return result + return sum(bool(antin.get(i)) for i in self.grid.keys()) def _calculate_1(self): return self._common(2, False) From df62406171768474eb7db0a5aa3bc96a116cff21 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 13:05:14 -0800 Subject: [PATCH 5/5] cleanup --- common/grid.py | 6 ++---- y_2024/day8.py | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/common/grid.py b/common/grid.py index 03e7135..5c26f88 100644 --- a/common/grid.py +++ b/common/grid.py @@ -18,7 +18,7 @@ def __post_init__(self) -> None: @staticmethod def from_input(input_data): - input = [[i for i in chunk] for chunk in input_data] + input = [list(chunk) for chunk in input_data] grid = {} for y in input: @@ -30,9 +30,7 @@ def from_input(input_data): def display(self) -> None: for i in range(self.height): - line = [] - for j in range(self.length): - line.append(self.grid[(i, j)]) + line = [self.grid[(i, j)] for j in range(self.length)] print("".join(line)) def items(self): diff --git a/y_2024/day8.py b/y_2024/day8.py index a8cdf2e..ca1fde0 100644 --- a/y_2024/day8.py +++ b/y_2024/day8.py @@ -10,31 +10,32 @@ def _preprocess_input(self): self.grid = Grid.from_input(self._input_data) self.grid.display() - def _anti2(self, a, b, i, include) -> list: - x = [] + def _get_antinodes(self, a, b, i, include) -> list: + x: list = [] + dist = (b[0] - a[0]), (b[1] - a[1]) start = 0 if include else 1 for i in range(start, i): x.extend( ( - (a[0] - i * (b[0] - a[0]), a[1] - i * (b[1] - a[1])), - (b[0] + i * (b[0] - a[0]), b[1] + i * (b[1] - a[1])), - ) + (a[0] - i * dist[0], a[1] - i * dist[1]), + (b[0] + i * dist[0], b[1] + i * dist[1]), + ), ) return x def _common(self, i, include): - antin = {} + antin = set() antennas = {k: v for k, v in self.grid.values.items() if k != "."} for j in antennas.values(): for x in range(len(j)): for y in range(len(j)): if j[x] == j[y]: continue - ants = self._anti2(j[x], j[y], i, include) + ants = self._get_antinodes(j[x], j[y], i, include) for m in ants: - antin[m] = "O" + antin.add(m) - return sum(bool(antin.get(i)) for i in self.grid.keys()) + return sum(bool(i in antin) for i in self.grid.keys()) def _calculate_1(self): return self._common(2, False)