Skip to content

Commit c82521a

Browse files
committed
[2024/18] p1 & p2 solved
1 parent 9252b53 commit c82521a

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

.aoc_tiles/tiles/2024/18.png

5.25 KB
Loading

2024/18/example

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
5,4
2+
4,2
3+
4,5
4+
3,0
5+
2,1
6+
6,3
7+
2,4
8+
1,5
9+
0,6
10+
3,3
11+
2,6
12+
5,1
13+
1,2
14+
5,5
15+
2,5
16+
6,5
17+
1,4
18+
0,4
19+
6,4
20+
1,1
21+
6,1
22+
1,0
23+
0,5
24+
1,6
25+
2,0

2024/18/script.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from GhostyUtils import aoc
2+
from GhostyUtils.grid import Grid
3+
from GhostyUtils.vec2 import Vec2, Dir
4+
from GhostyUtils.pathfinding import a_star, reconstruct_path
5+
from functools import partial
6+
7+
8+
aoc.argparser.add_argument("-s", "--size", type=int, default=71, help="size of 2d memory")
9+
aoc.argparser.add_argument("-b", "--bytes", type=int, default=1024, help="number of bytes to drop")
10+
11+
12+
def neighbours(pos: tuple, grid: Grid) -> list[tuple]:
13+
return list(cell for cell in grid.neighbours(pos, diagonal=False)
14+
if grid[cell] != '#')
15+
16+
17+
def main():
18+
inputs = aoc.read_lines()
19+
20+
memory = Grid(["."])
21+
memory.expand_for(Vec2(aoc.args.size-1, aoc.args.size-1), fill='.')
22+
23+
bytes_ = list(Vec2.from_str(line, split=',') for line in inputs)
24+
num_bytes = min(len(bytes_), aoc.args.bytes)
25+
for byte in bytes_[:num_bytes]:
26+
memory[byte] = '#'
27+
28+
start = (0, 0)
29+
end = (memory.width()-1, memory.height()-1)
30+
31+
neighbour_func = partial(neighbours, grid=memory)
32+
came_from, _, _ = a_star(start, end, neighbours=neighbour_func)
33+
path = reconstruct_path(came_from, start, end)
34+
35+
if aoc.args.verbose or aoc.args.progress:
36+
overlays = [{pos: 'O' for pos in path},
37+
{start: 'S', end: 'E'},]
38+
print(memory.render_with_overlays(overlays))
39+
print(f"p1: {len(path)-1}")
40+
41+
for byte in bytes_[num_bytes:]:
42+
memory[byte] = '#'
43+
came_from, _, _ = a_star(start, end, neighbours=neighbour_func)
44+
path = reconstruct_path(came_from, start, end)
45+
if len(path) == 0:
46+
print(f"p2: {byte[0]},{byte[1]}")
47+
break
48+
49+
50+
if __name__ == "__main__":
51+
main()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 193/484
6+
Advent of Code - 195/486
77
</h1>
88
<h1 align="center">
9-
2024 - 32 ⭐ - Python
9+
2024 - 34 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -59,6 +59,9 @@ My solutions to the yearly Advents of Code
5959
<a href="2024/17/script.py">
6060
<img src=".aoc_tiles/tiles/2024/17.png" width="161px">
6161
</a>
62+
<a href="2024/18/script.py">
63+
<img src=".aoc_tiles/tiles/2024/18.png" width="161px">
64+
</a>
6265
<h1 align="center">
6366
2023 - 47 ⭐ - Python
6467
</h1>

0 commit comments

Comments
 (0)