generated from public/aoc_template
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
35f2a3e4ae
BIN
.aoc_tiles/tiles/2024/14.png
Normal file
BIN
.aoc_tiles/tiles/2024/14.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.0 KiB |
BIN
.aoc_tiles/tiles/2024/15.png
Normal file
BIN
.aoc_tiles/tiles/2024/15.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.3 KiB |
BIN
.aoc_tiles/tiles/2024/16.png
Normal file
BIN
.aoc_tiles/tiles/2024/16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
11
README.md
11
README.md
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
2024 - 26 ⭐ - Python
|
2024 - 32 ⭐ - Python
|
||||||
</h1>
|
</h1>
|
||||||
<a href="day01.py">
|
<a href="day01.py">
|
||||||
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
|
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
|
||||||
@ -43,4 +43,13 @@
|
|||||||
<a href="day13.py">
|
<a href="day13.py">
|
||||||
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
|
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
|
||||||
</a>
|
</a>
|
||||||
|
<a href="day14.py">
|
||||||
|
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
|
||||||
|
</a>
|
||||||
|
<a href="day15.py">
|
||||||
|
<img src=".aoc_tiles/tiles/2024/15.png" width="161px">
|
||||||
|
</a>
|
||||||
|
<a href="day16.py">
|
||||||
|
<img src=".aoc_tiles/tiles/2024/16.png" width="161px">
|
||||||
|
</a>
|
||||||
<!-- AOC TILES END -->
|
<!-- AOC TILES END -->
|
||||||
38
day16.py
38
day16.py
@ -30,24 +30,39 @@ def get_best_score(grid: Grid) -> tuple[int, set[Coordinate]]:
|
|||||||
def get_tiles_from_all_paths(grid: Grid) -> int:
|
def get_tiles_from_all_paths(grid: Grid) -> int:
|
||||||
target_score, target_path = get_best_score(grid)
|
target_score, target_path = get_best_score(grid)
|
||||||
target = Coordinate(grid.maxX - 1, 1)
|
target = Coordinate(grid.maxX - 1, 1)
|
||||||
|
|
||||||
|
path_scores = {}
|
||||||
|
score = 0
|
||||||
|
facing = Coordinate(1, 0)
|
||||||
|
pos = Coordinate(1, grid.maxY - 1)
|
||||||
|
visited = set()
|
||||||
|
while pos != target:
|
||||||
|
visited.add(pos)
|
||||||
|
for n in pos.getNeighbours(includeDiagonal=False):
|
||||||
|
if n not in target_path or n in visited:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if n - pos != facing:
|
||||||
|
score += 1000
|
||||||
|
facing = n - pos
|
||||||
|
|
||||||
|
path_scores[pos] = score
|
||||||
|
pos = n
|
||||||
|
score += 1
|
||||||
|
|
||||||
queue = [(0, Coordinate(1, grid.maxY - 1), Coordinate(1, 0), {Coordinate(1, grid.maxY - 1)})]
|
queue = [(0, Coordinate(1, grid.maxY - 1), Coordinate(1, 0), {Coordinate(1, grid.maxY - 1)})]
|
||||||
# all_paths = set()
|
|
||||||
visited = {}
|
visited = {}
|
||||||
while queue:
|
while queue:
|
||||||
score, pos, facing, path = heappop(queue)
|
score, pos, facing, path = heappop(queue)
|
||||||
if pos in visited and score > visited[pos][0] + 1000:
|
if pos in visited and score > visited[pos] + 1000:
|
||||||
continue
|
continue
|
||||||
if pos in target_path and pos in visited:
|
|
||||||
# print(f"{pos} in target_path and visited. {score=}, {visited[pos]=}, discarding {path=}")
|
if pos in path_scores and pos in visited:
|
||||||
if score <= visited[pos][0] or score == visited[pos][0] + 1000:
|
if score <= path_scores[pos]:
|
||||||
target_path |= path
|
target_path |= path
|
||||||
continue
|
continue
|
||||||
|
|
||||||
visited[pos] = (score, facing)
|
visited[pos] = score
|
||||||
if pos == target:
|
|
||||||
target_path |= path
|
|
||||||
continue
|
|
||||||
|
|
||||||
if score >= target_score:
|
if score >= target_score:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -58,9 +73,6 @@ def get_tiles_from_all_paths(grid: Grid) -> int:
|
|||||||
add_turn = 1001 if n - pos != facing else 1
|
add_turn = 1001 if n - pos != facing else 1
|
||||||
heappush(queue, (score + add_turn, n, n - pos, path | {n}))
|
heappush(queue, (score + add_turn, n, n - pos, path | {n}))
|
||||||
|
|
||||||
for p in target_path:
|
|
||||||
grid.set(p, "O")
|
|
||||||
grid.print()
|
|
||||||
return len(target_path)
|
return len(target_path)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user