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