Skip to content

Commit

Permalink
AoC 2023 Day 21 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 21, 2023
1 parent 0141096 commit c8e2e7a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- @BEGIN:ImplementationsTable:2023@ -->
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | [](src/main/python/AoC2023_15.py) | [](src/main/python/AoC2023_16.py) | [](src/main/python/AoC2023_17.py) | [](src/main/python/AoC2023_18.py) | [](src/main/python/AoC2023_19.py) | [](src/main/python/AoC2023_20.py) | | | | | |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | [](src/main/python/AoC2023_15.py) | [](src/main/python/AoC2023_16.py) | [](src/main/python/AoC2023_17.py) | [](src/main/python/AoC2023_18.py) | [](src/main/python/AoC2023_19.py) | [](src/main/python/AoC2023_20.py) | [](src/main/python/AoC2023_21.py) | | | | |
| java | [](src/main/java/AoC2023_01.java) | [](src/main/java/AoC2023_02.java) | [](src/main/java/AoC2023_03.java) | [](src/main/java/AoC2023_04.java) | [](src/main/java/AoC2023_05.java) | [](src/main/java/AoC2023_06.java) | [](src/main/java/AoC2023_07.java) | [](src/main/java/AoC2023_08.java) | [](src/main/java/AoC2023_09.java) | [](src/main/java/AoC2023_10.java) | [](src/main/java/AoC2023_11.java) | [](src/main/java/AoC2023_12.java) | [](src/main/java/AoC2023_13.java) | [](src/main/java/AoC2023_14.java) | [](src/main/java/AoC2023_15.java) | [](src/main/java/AoC2023_16.java) | [](src/main/java/AoC2023_17.java) | [](src/main/java/AoC2023_18.java) | | [](src/main/java/AoC2023_20.java) | | | | | |
| rust | [](src/main/rust/AoC2023_01/src/main.rs) | [](src/main/rust/AoC2023_02/src/main.rs) | [](src/main/rust/AoC2023_03/src/main.rs) | [](src/main/rust/AoC2023_04/src/main.rs) | | [](src/main/rust/AoC2023_06/src/main.rs) | [](src/main/rust/AoC2023_07/src/main.rs) | [](src/main/rust/AoC2023_08/src/main.rs) | [](src/main/rust/AoC2023_09/src/main.rs) | | | | | | [](src/main/rust/AoC2023_15/src/main.rs) | [](src/main/rust/AoC2023_16/src/main.rs) | [](src/main/rust/AoC2023_17/src/main.rs) | [](src/main/rust/AoC2023_18/src/main.rs) | | | | | | | |
<!-- @END:ImplementationsTable:2023@ -->
Expand Down
54 changes: 35 additions & 19 deletions src/main/python/AoC2023_21.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from aoc.common import SolutionBase
from aoc.common import aoc_samples
from aoc.common import log
from aoc.grid import CharGrid, Cell
from aoc.grid import CharGrid

Input = CharGrid
Output1 = int
Expand All @@ -30,32 +30,48 @@
...........
"""

DIRS = {(0, 1), (0, -1), (1, 0), (-1, 0)}
STEPS = 26_501_365


class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return CharGrid.from_strings([_ for _ in input_data])

def part_1(self, grid: Input) -> Output1:
start = next(grid.get_all_equal_to("S"))
plots = set[Cell]([start])
for _ in range(64):
new_plots = set[Cell]()
for cell in plots:
for n in grid.get_capital_neighbours(cell):
if grid.get_value(n) != "#":
new_plots.add(n)
def solve(self, grid: CharGrid, steps: int) -> int:
w = grid.get_width()
start = (w // 2, w // 2)
plots = set[tuple[int, int]]([start])
for _ in range(steps):
new_plots = set[tuple[int, int]]()
for r, c in plots:
for dr, dc in DIRS:
rr, cc = r + dr, c + dc
wr, wc = rr % w, cc % w
if (
0 <= wr < w
and 0 <= wc < w
and grid.values[wr][wc] != "#"
):
new_plots.add((rr, cc))
plots = new_plots
return len(plots)

def part_2(self, input: Input) -> Output2:
return 0

@aoc_samples(
(
# ("part_1", TEST, 16),
# ("part_2", TEST, "TODO"),
)
)
def part_1(self, grid: Input) -> Output1:
return self.solve(grid, 64)

def part_2(self, grid: Input) -> Output2:
w = grid.get_width()
modulo = STEPS % w
x = STEPS // w
values = [self.solve(grid, i * w + modulo) for i in range(3)]
log(values)
a = (values[2] + values[0] - 2 * values[1]) // 2
b = values[1] - values[0] - a
c = values[0]
return a * x * x + b * x + c

@aoc_samples(())
def samples(self) -> None:
pass

Expand Down

0 comments on commit c8e2e7a

Please sign in to comment.