Solve 2024 day 10
This commit is contained in:
36
2024/d10.py
Normal file
36
2024/d10.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from collections import deque
|
||||
from lib import Grid2D
|
||||
from lib import get_data
|
||||
|
||||
|
||||
data = get_data(__file__)
|
||||
g = Grid2D(data)
|
||||
starts = g.find("0")
|
||||
p1, p2 = 0, 0
|
||||
for start in starts:
|
||||
n = 0
|
||||
vertex = ((start,), start)
|
||||
visited = set()
|
||||
queue = deque([vertex])
|
||||
nines = set()
|
||||
|
||||
while queue:
|
||||
path, pos = queue.popleft()
|
||||
if (path, pos) in visited:
|
||||
continue
|
||||
visited.add((path, pos))
|
||||
|
||||
if g[pos] == "9":
|
||||
n += 1
|
||||
nines.add(pos)
|
||||
continue
|
||||
|
||||
for nb in g.neighbors_ort(pos):
|
||||
if int(g[pos]) + 1 == int(g[nb]):
|
||||
npath = tuple(list(path) + [nb])
|
||||
queue.append((npath, nb))
|
||||
p1 += len(nines)
|
||||
p2 += n
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
19
README.md
19
README.md
@@ -291,13 +291,14 @@ and focus. Of course, learning more algorithms and techniques helps.
|
||||
|
||||
## AoC 2024
|
||||
|
||||
- Day 1: `00:01:30 124 0 00:02:49 141 0`
|
||||
- Day 2: `00:05:34 686 0 00:08:21 531 0`
|
||||
- Day 3: `00:01:48 165 0 00:02:40 56 45`
|
||||
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
|
||||
- Day 5: `00:07:07 679 0 00:23:02 1998 0`
|
||||
- Day 6: `00:09:43 1082 0 00:16:29 464 0`
|
||||
- Day 7: `00:12:29 2058 0 00:13:08 1170 0`
|
||||
- Day 8: `00:15:59 1742 0 00:26:56 2190 0`
|
||||
- Day 9: `00:48:23 6055 0 01:09:38 3159 0`
|
||||
- Day 1: `00:01:30 124 0 00:02:49 141 0`
|
||||
- Day 2: `00:05:34 686 0 00:08:21 531 0`
|
||||
- Day 3: `00:01:48 165 0 00:02:40 56 45`
|
||||
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
|
||||
- Day 5: `00:07:07 679 0 00:23:02 1998 0`
|
||||
- Day 6: `00:09:43 1082 0 00:16:29 464 0`
|
||||
- Day 7: `00:12:29 2058 0 00:13:08 1170 0`
|
||||
- Day 8: `00:15:59 1742 0 00:26:56 2190 0`
|
||||
- Day 9: `00:48:23 6055 0 01:09:38 3159 0`
|
||||
- Day 10: `00:19:47 2976 0 00:20:47 2244 0`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user