Skip to content

Commit

Permalink
[2024/18] p1 & p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 18, 2024
1 parent 9252b53 commit c82521a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions 2024/18/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
51 changes: 51 additions & 0 deletions 2024/18/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir
from GhostyUtils.pathfinding import a_star, reconstruct_path
from functools import partial


aoc.argparser.add_argument("-s", "--size", type=int, default=71, help="size of 2d memory")
aoc.argparser.add_argument("-b", "--bytes", type=int, default=1024, help="number of bytes to drop")


def neighbours(pos: tuple, grid: Grid) -> list[tuple]:
return list(cell for cell in grid.neighbours(pos, diagonal=False)
if grid[cell] != '#')


def main():
inputs = aoc.read_lines()

memory = Grid(["."])
memory.expand_for(Vec2(aoc.args.size-1, aoc.args.size-1), fill='.')

bytes_ = list(Vec2.from_str(line, split=',') for line in inputs)
num_bytes = min(len(bytes_), aoc.args.bytes)
for byte in bytes_[:num_bytes]:
memory[byte] = '#'

start = (0, 0)
end = (memory.width()-1, memory.height()-1)

neighbour_func = partial(neighbours, grid=memory)
came_from, _, _ = a_star(start, end, neighbours=neighbour_func)
path = reconstruct_path(came_from, start, end)

if aoc.args.verbose or aoc.args.progress:
overlays = [{pos: 'O' for pos in path},
{start: 'S', end: 'E'},]
print(memory.render_with_overlays(overlays))
print(f"p1: {len(path)-1}")

for byte in bytes_[num_bytes:]:
memory[byte] = '#'
came_from, _, _ = a_star(start, end, neighbours=neighbour_func)
path = reconstruct_path(came_from, start, end)
if len(path) == 0:
print(f"p2: {byte[0]},{byte[1]}")
break


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 - 193/484
Advent of Code - 195/486
</h1>
<h1 align="center">
2024 - 32 ⭐ - Python
2024 - 34 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -59,6 +59,9 @@ My solutions to the yearly Advents of Code
<a href="2024/17/script.py">
<img src=".aoc_tiles/tiles/2024/17.png" width="161px">
</a>
<a href="2024/18/script.py">
<img src=".aoc_tiles/tiles/2024/18.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit c82521a

Please sign in to comment.