forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp079.py
More file actions
53 lines (40 loc) · 1.34 KB
/
p079.py
File metadata and controls
53 lines (40 loc) · 1.34 KB
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
#
# Solution to Project Euler problem 79
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import itertools
def compute():
# Only guess characters that appear in the attempts
charsused = sorted(set().union(*SUBSEQS))
base = len(charsused)
# Try ascending lengths
for length in itertools.count(base):
indices = [0] * length
while True:
guess = "".join(charsused[d] for d in indices)
if is_consistent(guess):
return guess
# Increment indices
i = 0
while i < length and indices[i] == base - 1:
indices[i] = 0
i += 1
if i == length:
break
indices[i] += 1
def is_consistent(guess):
return all(is_subsequence(s, guess) for s in SUBSEQS)
def is_subsequence(shortstr, longstr):
i = 0
for c in longstr:
if c == shortstr[i]:
i += 1
if i == len(shortstr):
return True
return False
SUBSEQS = ["319", "680", "180", "690", "129", "620", "762", "689", "762", "318", "368", "710", "720", "710", "629", "168", "160", "689", "716", "731", "736", "729", "316", "729", "729", "710", "769", "290", "719", "680", "318", "389", "162", "289", "162", "718", "729", "319", "790", "680", "890", "362", "319", "760", "316", "729", "380", "319", "728", "716"]
if __name__ == "__main__":
print(compute())