generated from public/aoc_template
Compare commits
No commits in common. "7442fe9089e1c71a1215302d892ab8fc2a00e13e" and "7fc22b858a2e158c09f55d81642f708323a06fdb" have entirely different histories.
7442fe9089
...
7fc22b858a
Binary file not shown.
|
Before Width: | Height: | Size: 9.5 KiB |
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
2024 - 20 ⭐ - Python
|
2024 - 18 ⭐ - 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">
|
||||||
@ -31,7 +31,4 @@
|
|||||||
<a href="day09.py">
|
<a href="day09.py">
|
||||||
<img src=".aoc_tiles/tiles/2024/09.png" width="161px">
|
<img src=".aoc_tiles/tiles/2024/09.png" width="161px">
|
||||||
</a>
|
</a>
|
||||||
<a href="day10.py">
|
|
||||||
<img src=".aoc_tiles/tiles/2024/10.png" width="161px">
|
|
||||||
</a>
|
|
||||||
<!-- AOC TILES END -->
|
<!-- AOC TILES END -->
|
||||||
56
day10.py
56
day10.py
@ -6,39 +6,71 @@ from tools.grid import Grid
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def find_nine_ways(grid: Grid, start: Coordinate, next_val: int = 1) -> list[Coordinate]:
|
def find_nine(grid: Grid, start: Coordinate) -> int:
|
||||||
if next_val == 9:
|
q = deque([start])
|
||||||
return [x for x in grid.getNeighboursOf(start, includeDiagonal=False) if grid.get(x) == 9]
|
seen = set()
|
||||||
else:
|
nines = set()
|
||||||
ret = []
|
while q:
|
||||||
for x in grid.getNeighboursOf(start, includeDiagonal=False):
|
pos = q.popleft()
|
||||||
if grid.get(x) == next_val:
|
cur_val = grid.get(pos)
|
||||||
ret += find_nine_ways(grid, x, next_val + 1)
|
if cur_val == 9:
|
||||||
|
nines.add(pos)
|
||||||
|
continue
|
||||||
|
if pos in seen:
|
||||||
|
continue
|
||||||
|
seen.add(pos)
|
||||||
|
|
||||||
|
for next_pos in grid.getNeighboursOf(pos, includeDiagonal=False):
|
||||||
|
if grid.get(next_pos) == cur_val + 1:
|
||||||
|
q.append(next_pos)
|
||||||
|
|
||||||
|
return len(nines)
|
||||||
|
|
||||||
|
|
||||||
|
def find_nine_ways(grid: Grid, start: Coordinate, next_val: int = 1) -> int:
|
||||||
|
if next_val == 9:
|
||||||
|
return sum(1 for x in grid.getNeighboursOf(start, includeDiagonal=False) if grid.get(x) == 9)
|
||||||
|
else:
|
||||||
|
return sum(
|
||||||
|
find_nine_ways(grid, x, next_val + 1)
|
||||||
|
for x in grid.getNeighboursOf(start, includeDiagonal=False)
|
||||||
|
if grid.get(x) == next_val
|
||||||
|
)
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
class Day(AOCDay):
|
class Day(AOCDay):
|
||||||
inputs = [
|
inputs = [
|
||||||
[
|
[
|
||||||
|
(1, "input10_test2"),
|
||||||
|
(2, "input10_test3"),
|
||||||
|
(4, "input10_test4"),
|
||||||
|
(3, "input10_test5"),
|
||||||
(36, "input10_test"),
|
(36, "input10_test"),
|
||||||
(644, "input10"),
|
(644, "input10"),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
(3, "input10_test7"),
|
||||||
(81, "input10_test"),
|
(81, "input10_test"),
|
||||||
(1366, "input10"),
|
(1366, "input10"),
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
def parse_input(self) -> Grid:
|
def parse_input(self) -> Grid:
|
||||||
return Grid.from_data(self.getInput(), default=-1, translate={"[0-9]": int})
|
return Grid.from_data(self.getInput(), default=0, translate={"[0-9]": int})
|
||||||
|
|
||||||
def part1(self) -> Any:
|
def part1(self) -> Any:
|
||||||
grid = self.parse_input()
|
grid = self.parse_input()
|
||||||
return sum(len(set(find_nine_ways(grid, trail))) for trail in grid.find(0))
|
trail_heads = [
|
||||||
|
Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0
|
||||||
|
]
|
||||||
|
return sum(find_nine(grid, trail) for trail in trail_heads)
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
grid = self.parse_input()
|
grid = self.parse_input()
|
||||||
return sum(len(find_nine_ways(grid, trail)) for trail in grid.find(0))
|
trail_heads = [
|
||||||
|
Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0
|
||||||
|
]
|
||||||
|
return sum(find_nine_ways(grid, trail) for trail in trail_heads)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
4
inputs/input10_test2
Normal file
4
inputs/input10_test2
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
0123
|
||||||
|
1234
|
||||||
|
8765
|
||||||
|
9876
|
||||||
7
inputs/input10_test3
Normal file
7
inputs/input10_test3
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
...0...
|
||||||
|
...1...
|
||||||
|
...2...
|
||||||
|
6543456
|
||||||
|
7.....7
|
||||||
|
8.....8
|
||||||
|
9.....9
|
||||||
7
inputs/input10_test4
Normal file
7
inputs/input10_test4
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
..90..9
|
||||||
|
...1.98
|
||||||
|
...2..7
|
||||||
|
6543456
|
||||||
|
765.987
|
||||||
|
876....
|
||||||
|
987....
|
||||||
7
inputs/input10_test5
Normal file
7
inputs/input10_test5
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
10..9..
|
||||||
|
2...8..
|
||||||
|
3...7..
|
||||||
|
4567654
|
||||||
|
...8..3
|
||||||
|
...9..2
|
||||||
|
.....01
|
||||||
7
inputs/input10_test7
Normal file
7
inputs/input10_test7
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.....0.
|
||||||
|
..4321.
|
||||||
|
..5..2.
|
||||||
|
..6543.
|
||||||
|
..7..4.
|
||||||
|
..8765.
|
||||||
|
..9....
|
||||||
Loading…
Reference in New Issue
Block a user