Skip to content

Commit

Permalink
AoC 2023 Day 13 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 13, 2023
1 parent e399f57 commit 6025e0a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 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) | | | | | | | | | | | | | |
| 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) | | | | | | | | | | | | |
| 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) | | | | | | | | | | | | | |
| bash | | | | | | | | | | | | | | | | | | | | | | | | | |
| c++ | | | | | | | | | | | | | | | | | | | | | | | | | |
Expand Down
96 changes: 58 additions & 38 deletions src/main/python/AoC2023_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
from aoc.common import log
from aoc.grid import Cell
from aoc.grid import CharGrid

Expand Down Expand Up @@ -44,53 +43,74 @@ def parse_input(self, input_data: InputData) -> Input:
for block in my_aocd.to_blocks(input_data)
]

def solve(self, grid: CharGrid) -> list[tuple[int, int]]:
ans = []
h = grid.get_height()
w = grid.get_width()
for c in range(w - 1):
for dc in range(w):
left = c - dc
right = c + dc + 1
if 0 <= left < right < w:
for r in range(h):
if grid.get_value(Cell(r, left)) != grid.get_value(
Cell(r, right)
):
break
else:
continue
break
else:
ans.append((0, c + 1))
for r in range(h - 1):
for dr in range(h):
up = r - dr
down = r + dr + 1
if 0 <= up < down < h:
for c in range(w):
if grid.get_value(Cell(up, c)) != grid.get_value(
Cell(down, c)
):
break
else:
continue
break
else:
ans.append((r + 1, 0))
return ans

def part_1(self, grids: Input) -> Output1:
ans = 0
for grid in grids:
h = grid.get_height()
w = grid.get_width()
for c in range(w - 1):
for dc in range(w - c):
left = c - dc
right = c + dc + 1
if 0 <= left < right < w:
for r in range(h):
if grid.get_value(Cell(r, left)) != grid.get_value(
Cell(r, right)
):
break
else:
continue
break
else:
ans += c + 1
break
for r in range(h - 1):
for dr in range(h - r):
up = r - dr
down = r + dr + 1
if 0 <= up < down < h:
for c in range(w):
if grid.get_value(Cell(up, c)) != grid.get_value(
Cell(down, c)
):
break
else:
continue
syms = self.solve(grid)
assert len(syms) <= 1
ans += (100 * syms[0][0]) + syms[0][1]
return ans

def part_2(self, grids: Input) -> Output2:
ans = 0
for i, grid in enumerate(grids):
osyms = self.solve(grid)
g = CharGrid([[c for c in row] for row in grid.values])
for cell in g.get_cells():
tmp = g.get_value(cell)
g.set_value(cell, "." if tmp == "#" else "#")
syms = self.solve(g)
for s in syms:
if not osyms or s != osyms[0]:
ans += (100 * s[0]) + s[1]
break
else:
ans += 100 * (r + 1)
break
log(ans)
g.set_value(cell, tmp)
continue
g.set_value(cell, tmp)
break
return ans

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

@aoc_samples(
(
("part_1", TEST, 405),
# ("part_2", TEST, "TODO"),
("part_2", TEST, 400),
)
)
def samples(self) -> None:
Expand Down

0 comments on commit 6025e0a

Please sign in to comment.