-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day13_1.py
executable file
·64 lines (48 loc) · 1.25 KB
/
aoc23_day13_1.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
#!/usr/bin/env python
def read_diagram():
res = []
while True:
try:
line = input().strip()
if line == '':
return res
res.append(line)
except EOFError:
return res
def traspose(diagram):
res = []
for i in range(len(diagram[0])):
res.append(''.join([diagram[j][i] for j in range(len(diagram))]))
return res
def get_range(diagram, i):
res = 0
while True:
i1 = i - res
i2 = i + res + 1
if i1 >= 0 and i2 < len(diagram) and diagram[i1] == diagram[i2]:
res += 1
else:
break
return res
def find_mirror_point(diagram):
for i in range(len(diagram) - 1):
count = get_range(diagram, i)
if count != 0 and count == min((i + 1, len(diagram) - i - 1)):
return i
def main():
res = 0
while True:
diagram = read_diagram()
if not diagram:
break
x = find_mirror_point(diagram)
if x is not None:
res += 100 * (x + 1)
else:
tr = traspose(diagram)
y = find_mirror_point(tr)
assert y is not None
res += (y + 1)
print(res)
if __name__ == '__main__':
main()