-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler-p59.py
More file actions
69 lines (57 loc) · 1.91 KB
/
Copy patheuler-p59.py
File metadata and controls
69 lines (57 loc) · 1.91 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 7 14:57:47 2025
@author: phoeb
"""
######### 暴力解
########## https://walkerdoescode.home.blog/2019/10/18/project-euler-problem-59/
import time
f = open("D:/Downloads/0059_cipher.txt", "r")
if(f.mode == "r"):
contents = f.readlines()
realContents = []
for x in contents:
realContents.append(x.split(","))
else:
raise ValueError("Cannot read from file")
finalContents = []
for x in realContents[0]:
finalContents.append(int(x))
def isAcceptable(myChar):
return 'a'<=myChar<='z' or 'A'<=myChar<='Z' or myChar == ' ' or myChar == '\'' or myChar == ',' or myChar == '"' or myChar == '[' or myChar == ']' or myChar == ':' or '0'<=myChar<='9' or myChar == '/' or myChar == '.' or myChar == '(' or myChar == ')' or myChar == ';' or myChar=='+'
def projectEulerProblemFiftyNine(cipherText):
tl = len(cipherText)
alphabet = "abcdefghijklmnopqrstuvwxyz"
possible = []
for a in alphabet:
for b in alphabet:
for c in alphabet:
myGuess = a+b+c
asciiIndices = [ord(a),ord(b),ord(c)]
s = ""
bad = False
for x in range(tl):
myChar = chr(cipherText[x] ^ asciiIndices[x%3])
if not isAcceptable(myChar):
bad = True
break
s+=myChar
if not bad:
possible.append(s)
total = -1
for x in possible:
subTotal = 0
for y in x:
subTotal+=ord(y)
if(total==-1):
total = subTotal
return total
start = time.time()
print (projectEulerProblemFiftyNine(finalContents))
print ("--- %s seconds ---" % (time.time()-start))
'''
Prints
129448
--- 0.169327974319 seconds ---
for input of given cipher text.
'''