-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem59.py
More file actions
33 lines (26 loc) · 999 Bytes
/
Problem59.py
File metadata and controls
33 lines (26 loc) · 999 Bytes
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
from collections import Counter
def Solve():
file = open('./data/p059_cipher.txt')
cipherText = file.read()
file.close()
cipherText = [int(x) for x in cipherText.split(',')]
length = len(cipherText)
commonChar = ' ' #assuming the plan text is english, spaces are more prevalent than any letter
temp = 0
for charSet in range(3):
cipher = cipherText[charSet:length:3]
commonCharCount = 0
pwCode = 0
for pwLetter in range(ord('a'), ord('z')+1):
charCount = Counter()
plain = [chr(pwLetter ^ cLetter) for cLetter in cipher]
for plainLetter in plain:
charCount[plainLetter] += 1
if charCount[commonChar] > commonCharCount:
commonCharCount = charCount[commonChar]
pwCode = pwLetter
plainValue = sum([pwCode ^ cLetter for cLetter in cipher])
temp += plainValue
return temp
if __name__ == '__main__':
print(Solve())