Skip to content

Commit

Permalink
[2024/16] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 16, 2024
1 parent a65716a commit 87d5888
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions 2024/16/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
56 changes: 56 additions & 0 deletions 2024/16/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from GhostyUtils import aoc, pathfinding
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir
from functools import partial


def neighbours(pos: tuple[tuple[int], Dir], maze: Grid):
pos, dir = pos
return [(n, (Vec2(n) - Vec2(pos)).as_tuple())
for n in filter(lambda p: maze[p] != '#', maze.neighbours(pos, diagonal=False))]


def cost(current: tuple[tuple[int], Dir], next_: tuple[tuple[int], Dir]):
_, c_dir = current
_, n_dir = next_

if c_dir == n_dir:
return 1
elif Dir(n_dir) in {Dir(c_dir).turn_left(), Dir(c_dir).turn_right()}:
return 1000 + 1
else:
return 2001


def heuristic(next_pos: tuple[tuple[int], Dir], end: tuple[int]) -> int:
next_pos, _ = next_pos
return Vec2.manhattan_distance(next_pos, end)


def early_out(current, end):
return current[0] == end


def main():
maze = Grid(aoc.read_lines())
start = (maze.find('S'), Dir.EAST.as_tuple())
end = maze.find('E')

neighbours_func = partial(neighbours, maze=maze)
early_out_func = partial(early_out, end=end)
came_from, cost_so_far, last_pos = pathfinding.a_star(start, end,
neighbours=neighbours_func,
cost=cost,
heuristic=heuristic,
early_out=early_out_func)
path = pathfinding.reconstruct_path(came_from, start, last_pos)

if aoc.args.progress or aoc.args.verbose:
path_overlay = {pos: {Dir.N: '^', Dir.S: 'v', Dir.E: '>', Dir.W: '<'}[Dir(dir)]
for pos, dir in path}
print(maze.render_with_overlays([path_overlay]))
print(f"p1: {cost_so_far[last_pos]}")


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 191/480
Advent of Code - 192/482
</h1>
<h1 align="center">
2024 - 30 ⭐ - Python
2024 - 31 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -53,6 +53,9 @@ My solutions to the yearly Advents of Code
<a href="2024/15/script.py">
<img src=".aoc_tiles/tiles/2024/15.png" width="161px">
</a>
<a href="2024/16/script.py">
<img src=".aoc_tiles/tiles/2024/16.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit 87d5888

Please sign in to comment.