-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler068.py
59 lines (49 loc) · 1.63 KB
/
euler068.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
"""
Let's refer to these index-position relation:
0
\
1 3
* * /
8 2
/ \ /
9 \ /
6 ------- 4 ---- 5
\
7
"""
import itertools
from typing import List
def is_valid_order(sequence: List[int]) -> bool:
if sequence[0] == min([sequence[0], sequence[3], sequence[5], sequence[7], sequence[9]]):
return True
else:
return False
def is_valid_pentagon_ring(sequence: List[int]) -> bool:
assert sorted(sequence) == list(range(1, 11))
indices = [
[0, 1, 2],
[3, 2, 4],
[5, 4, 6],
[7, 6, 8],
[9, 8, 1]
]
i, j, k = indices[0]
total = sequence[i] + sequence[j] + sequence[k]
for i, j, k in indices[1:]:
total_new = sequence[i] + sequence[j] + sequence[k]
if total_new != total:
return False
return True
def full_reading_sequence(sequence: List[int]) -> List[int]:
assert len(sequence) == 10
return [sequence[i] for i in [0, 1, 2, 3, 2, 4, 5, 4, 6, 7, 6 ,8, 9, 8, 1]]
if __name__ == '__main__':
best_sequence = 0
for sequence in itertools.permutations(range(1, 11)):
if is_valid_order(list(sequence)):
if is_valid_pentagon_ring(list(sequence)):
sequence_str = "".join(str(i) for i in full_reading_sequence(list(sequence)))
if len(sequence_str) == 16:
if int(sequence_str) > best_sequence:
best_sequence = int(sequence_str)
print(f"Greatest 16-digits valid sequence {best_sequence}")