-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp059.py
More file actions
43 lines (37 loc) · 1.22 KB
/
Copy pathp059.py
File metadata and controls
43 lines (37 loc) · 1.22 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
from itertools import combinations_with_replacement as cwr
from itertools import permutations
if __name__ == '__main__':
with open('../data/p059_cipher.txt', 'r') as f:
line = f.readline() # only single line in the file
numbers = line.rstrip().split(',')
numbers = [int(n) for n in numbers]
length = len(numbers)
print(length)
charset = [' ', ',', '.']
st = ord('A')
en = st + 25
for i in range(st, en+1):
charset.append(chr(i))
st = ord('a')
en = st + 25
for i in range(st, en+1):
charset.append(chr(i))
print(''.join(charset))
for tup in cwr(list(range(st, en+1)), 3):
for t in permutations(tup):
s = []
for i, c in enumerate(numbers):
# print(chr(numbers[i]^t[i%3]), end='')
s.append(chr(c ^ t[i%3]))
cnt = 0
for c in s:
if not c in charset:
cnt += 1
if cnt < 50:
print('Key: ' + ''.join(map(chr, t))+' Errors: '+str(cnt))
print(''.join(s[0:100]))
res = 0
for c in s:
res += ord(c)
print('Ans: %d' % res)
break