forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic-square-forming.py
35 lines (29 loc) · 926 Bytes
/
magic-square-forming.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
#!/bin/python3
import sys
pre = [
[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
]
def formingMagicSquare(s):
results = []
for p in pre:
total = 0
for p_row, s_row in zip(p, s):
for i, j in zip(p_row, s_row):
if not i == j:
total += max([i, j]) - min([i, j])
results.append(total)
return min(results)
if __name__ == "__main__":
s = []
for s_i in range(3):
s_t = [int(s_temp) for s_temp in input().strip().split(' ')]
s.append(s_t)
result = formingMagicSquare(s)
print(result)