-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (80 loc) · 3.27 KB
/
main.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
with open("input.txt") as file:
LINES = [line.split("\n") for line in file.read().split("\n\n")]
total = 0
for mirror in LINES:
row_reflection_possible = False
for reflection_line in range(len(mirror) - 1):
reflection_line_possible = True
for i in range(min(len(mirror) - reflection_line - 1, reflection_line + 1)):
if mirror[reflection_line + i + 1] != mirror[reflection_line - i]:
reflection_line_possible = False
break
if reflection_line_possible:
total += (reflection_line + 1) * 100
row_reflection_possible = True
break
if not row_reflection_possible:
for reflection_col in range(len(mirror[0]) - 1):
reflection_col_possible = True
for i in range(
min(len(mirror[0]) - reflection_col - 1, reflection_col + 1)
):
for row in mirror:
if row[reflection_col + i + 1] != row[reflection_col - i]:
reflection_col_possible = False
break
if not reflection_col_possible:
break
if reflection_col_possible:
total += reflection_col + 1
break
print(total)
def wrong_chars(str1, str2):
return sum(a != b for a, b in zip(str1, str2))
total = 0
for mirror in LINES:
row_reflection_possible = False
for reflection_line in range(len(mirror) - 1):
reflection_line_possible = True
found_one_wrong = False
for i in range(min(len(mirror) - reflection_line - 1, reflection_line + 1)):
wrong_counter = wrong_chars(
mirror[reflection_line + i + 1], mirror[reflection_line - i]
)
if wrong_counter > 1:
reflection_line_possible = False
break
if wrong_counter == 1:
if found_one_wrong:
reflection_line_possible = False
break
else:
found_one_wrong = True
if reflection_line_possible and found_one_wrong:
total += (reflection_line + 1) * 100
row_reflection_possible = True
break
if not row_reflection_possible:
for reflection_col in range(len(mirror[0]) - 1):
reflection_col_possible = True
found_one_wrong = False
for i in range(
min(len(mirror[0]) - reflection_col - 1, reflection_col + 1)
):
wrong_counter = wrong_chars(
[row[reflection_col + i + 1] for row in mirror],
[row[reflection_col - i] for row in mirror],
)
if wrong_counter > 1:
reflection_col_possible = False
break
if wrong_counter == 1:
if found_one_wrong:
reflection_col_possible = False
break
else:
found_one_wrong = True
if reflection_col_possible and found_one_wrong:
total += reflection_col + 1
break
print(total)