-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path68_magic_5_gon_ring.py
145 lines (122 loc) · 2.69 KB
/
68_magic_5_gon_ring.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# () => () => ()
# I / |
# () ()
# I
# ()
## array with positions
pos_gen = [
(1, 0),
(1, 1),
(1, 2),
(0, 2),
(2, 1),
(2, 2),
(3, 1),
(3, 2),
(3, 0),
(2, 0),
]
## array with the directions
pos = [
(1, 0),
(1, 1),
(1, 2),
(0, 2),
(1, 2),
(2, 1),
(2, 2),
(2, 1),
(3, 1),
(3, 2),
(3, 1),
(3, 0),
(2, 0),
(3, 0),
(1, 1),
]
## store the solutions as a string in a set
sols = set()
def is_valid(arr):
p_sum = 0
sum = 0
for i, p in enumerate(pos):
if i > 0 and i % 3 == 0:
if p_sum != 0 and p_sum != sum:
return False
p_sum = sum
sum = 0
e = arr[p[0]][p[1]]
sum += e
if p_sum != sum:
return False
return True
def pretty_print(array):
s = ""
for p in pos:
separator = "" if len(s) == 0 else ","
s = f"{s}{separator}{array[p[0]][p[1]]}"
print(s)
def store_solution(array):
store = []
sv = ""
a = array[1][0]
b = array[0][2]
c = array[2][2]
d = array[3][2]
e = array[2][0]
s = 0
m = min([a, b, c, d, e])
if m == a:
s = 0
if m == b:
s = 3
if m == c:
s = 6
if m == d:
s = 9
if m == e:
s = 12
for i in range(s, s + len(pos)):
p = pos[i % len(pos)]
if i > 0 and i % 3 == 0:
store.append(sv)
sv = ""
sv = f"{sv}{array[p[0]][p[1]]}"
store.append(sv)
string_sol = "".join(store)
sols.add(string_sol)
def available_digits(array, c_pos):
existing_digits = []
for p in pos:
if p == c_pos:
break
existing_digits.append(array[p[0]][p[1]])
return [item for item in range(1, 11) if item not in existing_digits]
def gen_ring(j, array=[[], [], []]):
if j >= len(pos_gen):
if is_valid(array):
store_solution(array)
pretty_print(array)
return
c_pos = pos_gen[j]
for i in available_digits(array, c_pos):
array[c_pos[0]][c_pos[1]] = i
gen_ring(
j + 1,
array=array,
)
def compute_ring():
arr = [[None] * 3 for _ in range(4)]
for i, _ in enumerate(pos_gen):
gen_ring(i, arr)
def biggest_solution():
largest = ""
for i in sols:
## Due to the fact that some number can contain the number 10 multiple times
## they have different length. Therefore, compare strings
if i > largest:
largest = i
return largest
compute_ring()
print(biggest_solution()) # 6531031914842725
# print(is_valid([[None, None, 10], [6, 5, 3], [7, 1, 9], [2, 4, 8]]))