-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day3_2.py
executable file
·84 lines (62 loc) · 1.88 KB
/
aoc23_day3_2.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
#!/usr/bin/env python
def read_schematic():
res = []
while True:
try:
res.append(input().strip())
except EOFError:
break
return res
def expand_adj(schem, i, j):
res = []
for step_i, step_j in ((-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1),
(1, -1), (1, 1)):
new_i = i + step_i
new_j = j + step_j
if new_i >= 0 and new_i < len(schem) and new_j >= 0 \
and new_j < len(schem[0]):
res.append((new_i, new_j))
return res
def find_adj_gears(schem, coords):
res = []
for (i1, j1) in coords:
for (i2, j2) in expand_adj(schem, i1, j1):
if schem[i2][j2] == '*':
res.append((i2, j2))
return list(set(res))
def find_valid_number(schem, i, j, visited):
coords = []
num = 0
line = schem[i]
if not schem[i][j].isdigit():
return None, []
k = j
while k < len(line) and line[k].isdigit():
num = num * 10 + int(line[k])
coords.append((i, k))
visited[i][k] = True
k += 1
return num, find_adj_gears(schem, coords)
def main():
schem = read_schematic()
visited = [[False] * len(schem[0]) for _ in range(len(schem))]
gears_ratio = {}
for i, line in enumerate(schem):
for j, char in enumerate(line):
if visited[i][j]:
continue
num, gears = find_valid_number(schem, i, j, visited)
if num is None:
continue
for gear in gears:
if gear not in gears_ratio:
gears_ratio[gear] = [0, 1]
gears_ratio[gear][0] += 1
gears_ratio[gear][1] *= num
res = 0
for count, ratio in gears_ratio.values():
if count == 2:
res += ratio
print(res)
if __name__ == '__main__':
main()