-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_13_part_1.py
76 lines (69 loc) · 2.05 KB
/
day_13_part_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
65
66
67
68
69
70
71
72
73
74
75
76
from icecream import ic
def solve(m):
# search col
cand = 1
width = len(m[0])
height = len(m)
while cand < width:
is_mirror_col = True
distance_from_end = width - cand
min_x = max(0, width - 2 * distance_from_end)
max_x = min(width, cand)
for y in range(len(m)):
for x in range(min_x, max_x):
left_val = m[y][x]
right_x = cand + (cand - x) - 1
right_val = m[y][right_x]
# ic(cand, x, right_x, left_val, right_val)
if left_val != right_val:
# ic('not mirror')
is_mirror_col = False
break
if not is_mirror_col:
break
if is_mirror_col:
# ic('found')
return cand
cand += 1
# search row if col not found
cand = 1
while cand < height:
is_mirror = True
distance_from_end = height - cand
min_y = max(0, height - 2 * distance_from_end)
max_y = min(height, cand)
for y in range(min_y, max_y):
for x in range(width):
up_val = m[y][x]
down_y = cand + (cand - y) - 1
down_val = m[down_y][x]
# ic(cand, x, down_y, up_val, down_val)
if up_val != down_val:
# ic('not mirror')
is_mirror = False
break
if not is_mirror:
break
if is_mirror:
# ic('found')
return cand * 100
cand += 1
assert False # should not reach here
def main():
l = []
cur = []
with open('data/day13.data') as f:
for row in f:
if len(row.strip()) == 0:
l.append(cur)
cur = []
continue
cur.append([c for c in row.strip()])
l.append(cur)
res = 0
for pattern in l:
# ic(pattern)
res += solve(pattern)
ic(res)
if __name__ == '__main__':
main()