diff --git a/.aoc_tiles/tiles/2024/14.png b/.aoc_tiles/tiles/2024/14.png new file mode 100644 index 0000000..d8ebf64 Binary files /dev/null and b/.aoc_tiles/tiles/2024/14.png differ diff --git a/.aoc_tiles/tiles/2024/15.png b/.aoc_tiles/tiles/2024/15.png new file mode 100644 index 0000000..90096f9 Binary files /dev/null and b/.aoc_tiles/tiles/2024/15.png differ diff --git a/.aoc_tiles/tiles/2024/16.png b/.aoc_tiles/tiles/2024/16.png new file mode 100644 index 0000000..d41e1c0 Binary files /dev/null and b/.aoc_tiles/tiles/2024/16.png differ diff --git a/README.md b/README.md index 5e46822..b94e037 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

- 2024 - 26 ⭐ - Python + 2024 - 32 ⭐ - Python

@@ -43,4 +43,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/day16.py b/day16.py index fa20c11..842dab8 100644 --- a/day16.py +++ b/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: target_score, target_path = get_best_score(grid) 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)})] - # all_paths = set() visited = {} while 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 - if pos in target_path and pos in visited: - # print(f"{pos} in target_path and visited. {score=}, {visited[pos]=}, discarding {path=}") - if score <= visited[pos][0] or score == visited[pos][0] + 1000: + + if pos in path_scores and pos in visited: + if score <= path_scores[pos]: target_path |= path continue - visited[pos] = (score, facing) - if pos == target: - target_path |= path - continue - + visited[pos] = score if score >= target_score: continue @@ -58,9 +73,6 @@ def get_tiles_from_all_paths(grid: Grid) -> int: add_turn = 1001 if n - pos != facing else 1 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)