-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.py
128 lines (99 loc) · 3.29 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import time
from collections import deque
def clear_console():
os.system('cls' if os.name == 'nt' else 'clear')
def parse_input(filename):
with open(filename, 'r') as f:
return [list(map(int, line.strip())) for line in f if line.strip()]
def get_neighbors(grid, row, col):
moves = [(0, 1), (1, 0), (0, -1), (-1, 0)]
neighbors = []
for dr, dc in moves:
r, c = row + dr, col + dc
if 0 <= r < len(grid) and 0 <= c < len(grid[0]):
if grid[r][c] == grid[row][col] + 1:
neighbors.append((r, c))
return neighbors
def find_reachable_peaks(grid, start_row, start_col):
peaks = set()
queue = deque([(start_row, start_col)])
seen = {(start_row, start_col)}
while queue:
row, col = queue.popleft()
if grid[row][col] == 9:
peaks.add((row, col))
continue
for nr, nc in get_neighbors(grid, row, col):
if (nr, nc) not in seen:
seen.add((nr, nc))
queue.append((nr, nc))
return len(peaks)
def find_trailheads(grid):
trailheads = []
rows, cols = len(grid), len(grid[0])
for r in range(rows):
for c in range(cols):
if grid[r][c] == 0:
trailheads.append((r, c))
return trailheads
def find_all_paths(grid, start_row, start_col):
paths = []
curr_path = [(start_row, start_col)]
def dfs(row, col):
if grid[row][col] == 9:
paths.append(curr_path[:])
return
for nr, nc in get_neighbors(grid, row, col):
if (nr, nc) not in curr_path:
curr_path.append((nr, nc))
dfs(nr, nc)
curr_path.pop()
dfs(start_row, start_col)
return len(paths)
def part_one(grid):
trailheads = find_trailheads(grid)
total = len(trailheads)
processed = 0
scores = []
for row, col in trailheads:
score = find_reachable_peaks(grid, row, col)
scores.append(score)
processed += 1
if processed % 10 == 0:
clear_console()
print(f"Progress: {processed}/{total} trailheads processed ({processed/total:.1%})")
return sum(scores)
def part_two(grid):
trailheads = find_trailheads(grid)
total = len(trailheads)
processed = 0
ratings = []
for row, col in trailheads:
rating = find_all_paths(grid, row, col)
ratings.append(rating)
processed += 1
if processed % 1 == 0:
clear_console()
print(f"Progress: {processed}/{total} trailheads processed ({processed/total:.1%})")
print(f"Current trailhead rating: {rating}")
return sum(ratings)
def main():
start_time = time.time()
grid = parse_input('input.txt')
print("\n=== Part 1 ===")
part1_start = time.time()
result1 = part_one(grid)
part1_time = time.time() - part1_start
print("\n=== Part 2 ===")
part2_start = time.time()
result2 = part_two(grid)
part2_time = time.time() - part2_start
total_time = time.time() - start_time
clear_console()
print("=== Final Results ===")
print(f"Part 1: {result1} (took {part1_time:.2f}s)")
print(f"Part 2: {result2} (took {part2_time:.2f}s)")
print(f"\nTotal time: {total_time:.2f}s")
if __name__ == "__main__":
main()