-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoSolver.py
87 lines (79 loc) · 3.28 KB
/
GeoSolver.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
from decryptTools import score
ciphertext = '--.-.-.-.- -.....-... ...-..-..... .....-..- -..-. ---- .......-.-. -..-. ..-.---.-. --.----.. ...--- .-..---...-.-.. -..... .-----.-..-..-.. -.....-- ..... --..-...-. ......... ----.. .--.-.. ----..-..-.-- ...----. -.....-- .--....---....-..-. -.....-........-.... ..-. ......-- ........-.-...-.. -.---- .--...-.......... -.....-- .....-...-. .-..-.-..-.-.. .-......-.. ..-.---.-. --.----.. -....-.. -.---- ....-.-.. ......... ...----. ..-.---- -..... .-----.-..-..-.. ---- -.-.----.-...---. -..... .-----.-..-..-.. -.....-- ---- ....-...-. -..... .-----.-..-..-.. -.....-.---..---..... ......--'
conversion = {'..-.': 'F', '-..-': 'X', '.--.': 'P', '-': 'T', '..---': '2',
'....-': '4', '-----': '0', '--...': '7', '...-': 'V',
'-.-.': 'C', '.': 'E', '.---': 'J', '---': 'O', '-.-': 'K',
'----.': '9', '..': 'I', '.-..': 'L', '.....': '5', '...--': '3',
'-.--': 'Y', '-....': '6', '.--': 'W', '....': 'H', '-.': 'N',
'.-.': 'R', '-...': 'B', '---..': '8', '--..': 'Z', '-..': 'D',
'--.-': 'Q', '--.': 'G', '--': 'M', '..-': 'U', '.-': 'A',
'...': 'S', '.----': '1'}
s = set(conversion)
al = '|'+'|'.join(s)+'|'
def search(i=0, prev=''):
branch = {}
for nex in '.-':
if '|'+prev+nex in al:
branch[nex] = search(i+1, prev+nex)
if prev in s:
branch['char']=conversion[prev]
else:
branch['char']=None
return branch
tree = search()
def generate_endings(rem, branch=tree, incomplete=False):
if not rem:
if incomplete:
return []
else:
return ['']
possibilities = []
nex = rem[0]
rest = rem[1:]
if nex in branch:
limb = branch[nex]
char = limb['char']
if limb['char']:
endings = generate_endings(rest, tree)
if endings:
possibilities += [char + other for other in endings]
endings = generate_endings(rest, limb, True)
if endings:
possibilities += endings
return possibilities
chunks = ciphertext.split(' ')
poss_list = []
for c, chunk in enumerate(chunks):
print()
print(chunk)
print()
remaining = ' '.join(chunks[c:])
poss_list.append(generate_endings(chunk))
sort = sorted(poss_list[-1],
key=lambda x:(len([c for c in x if c not in '1234567890'])/
float(len(x)),
score(x)/(len(x)**1.0)),
# increase 1.0 to make shorter words sort closer to the start
# and decrease to make longer words come sooner
reverse = True
)
i = 0
cont = True
while cont:
try:
line = sort[i]
except IndexError:
print('no more possibilites to show.')
break
i += 1
while True:
try:
newline = line+' '+sort[i]
except IndexError:
break
if len(newline) > 80:
break
line = newline
i += 1
print(line)
cont = input('Press enter to continue, or type more to show more: ')