-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
52 lines (42 loc) · 1.34 KB
/
part2.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
def find_mirror(note: str, width: int, height: int, discard_result=None) -> int | None:
# Find vertical mirroring
for x in range(1, width):
if x == discard_result:
continue
if all(
note[y][x - i - 1] == note[y][x + i]
for y in range(height)
for i in range(min(x, width - x))
):
return x
# Find horizontal mirroring
for y in range(1, height):
if y * 100 == discard_result:
continue
if all(
note[y - i - 1][x] == note[y + i][x]
for x in range(width)
for i in range(min(y, height - y))
):
return y * 100
with open('input.txt') as file:
notes = [
list(map(list, note.splitlines()))
for note in file.read().split('\n\n')
]
answer = 0
for note in notes:
width, height = len(note[0]), len(note)
cur_mirror = find_mirror(note, width, height)
for y in range(height):
for x in range(width):
note[y][x] = '.' if note[y][x] == '#' else '#'
new_mirror = find_mirror(note, width, height, cur_mirror)
if new_mirror is not None:
answer += new_mirror
break
note[y][x] = '.' if note[y][x] == '#' else '#'
else:
continue
break
print(answer)