Skip to content

Commit 356e976

Browse files
fpringlegithub-actions
and
github-actions
authored
Add solution for Project Euler problem 59 (TheAlgorithms#4031)
* Added solution for Project Euler problem 59 * updating DIRECTORY.md * Formatting, type hints, no more evil map functions * Doctests * Added doctests for Project Euler problem 59 Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 6793cae commit 356e976

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

Diff for: DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@
345345

346346
## Linear Algebra
347347
* Src
348+
* [Conjugate Gradient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/conjugate_gradient.py)
348349
* [Lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/lib.py)
349350
* [Polynom For Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom_for_points.py)
350351
* [Power Iteration](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/power_iteration.py)
@@ -695,6 +696,8 @@
695696
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_057/sol1.py)
696697
* Problem 058
697698
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_058/sol1.py)
699+
* Problem 059
700+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_059/sol1.py)
698701
* Problem 062
699702
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py)
700703
* Problem 063

Diff for: project_euler/problem_059/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_059/p059_cipher.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
36,22,80,0,0,4,23,25,19,17,88,4,4,19,21,11,88,22,23,23,29,69,12,24,0,88,25,11,12,2,10,28,5,6,12,25,10,22,80,10,30,80,10,22,21,69,23,22,69,61,5,9,29,2,66,11,80,8,23,3,17,88,19,0,20,21,7,10,17,17,29,20,69,8,17,21,29,2,22,84,80,71,60,21,69,11,5,8,21,25,22,88,3,0,10,25,0,10,5,8,88,2,0,27,25,21,10,31,6,25,2,16,21,82,69,35,63,11,88,4,13,29,80,22,13,29,22,88,31,3,88,3,0,10,25,0,11,80,10,30,80,23,29,19,12,8,2,10,27,17,9,11,45,95,88,57,69,16,17,19,29,80,23,29,19,0,22,4,9,1,80,3,23,5,11,28,92,69,9,5,12,12,21,69,13,30,0,0,0,0,27,4,0,28,28,28,84,80,4,22,80,0,20,21,2,25,30,17,88,21,29,8,2,0,11,3,12,23,30,69,30,31,23,88,4,13,29,80,0,22,4,12,10,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,65,69,83,80,84,87,68,69,83,80,84,87,73,69,83,80,84,87,65,83,88,91,69,29,4,6,86,92,69,15,24,12,27,24,69,28,21,21,29,30,1,11,80,10,22,80,17,16,21,69,9,5,4,28,2,4,12,5,23,29,80,10,30,80,17,16,21,69,27,25,23,27,28,0,84,80,22,23,80,17,16,17,17,88,25,3,88,4,13,29,80,17,10,5,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,12,11,80,10,26,4,4,17,30,0,28,92,69,30,2,10,21,80,12,12,80,4,12,80,10,22,19,0,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,69,30,31,9,20,31,18,11,94,69,54,17,8,29,28,28,84,80,44,88,24,4,14,21,69,30,31,16,22,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,25,22,88,17,69,11,25,29,12,24,69,8,17,23,12,80,10,30,80,17,16,21,69,11,1,16,25,2,0,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,23,22,69,12,24,0,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,67,80,10,10,80,7,1,80,21,13,4,17,17,30,2,88,4,13,29,80,22,13,29,69,23,22,69,12,24,12,11,80,22,29,2,12,29,3,69,29,1,16,25,28,69,12,31,69,11,92,69,17,4,69,16,17,22,88,4,13,29,80,23,25,4,12,23,80,22,9,2,17,80,70,76,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,11,80,17,23,80,84,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,12,31,69,12,24,0,88,20,12,25,29,0,12,21,23,86,80,44,88,7,12,20,28,69,11,31,10,22,80,22,16,31,18,88,4,13,25,4,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,17,23,80,7,29,80,4,8,0,23,23,8,12,21,17,17,29,28,28,88,65,75,78,68,81,65,67,81,72,70,83,64,68,87,74,70,81,75,70,81,67,80,4,22,20,69,30,2,10,21,80,8,13,28,17,17,0,9,1,25,11,31,80,17,16,25,22,88,30,16,21,18,0,10,80,7,1,80,22,17,8,73,88,17,11,28,80,17,16,21,11,88,4,4,19,25,11,31,80,17,16,21,69,11,1,16,25,2,0,88,2,10,23,4,73,88,4,13,29,80,11,13,29,7,29,2,69,75,94,84,76,65,80,65,66,83,77,67,80,64,73,82,65,67,87,75,72,69,17,3,69,17,30,1,29,21,1,88,0,23,23,20,16,27,21,1,84,80,18,16,25,6,16,80,0,0,0,23,29,3,22,29,3,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,35,23,28,9,23,7,12,22,23,69,25,23,4,17,30,69,12,24,0,88,3,4,21,21,69,11,4,0,8,3,69,26,9,69,15,24,12,27,24,69,49,80,13,25,20,69,25,2,23,17,6,0,28,80,4,12,80,17,16,25,22,88,3,16,21,92,69,49,80,13,25,6,0,88,20,12,11,19,10,14,21,23,29,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,29,80,22,29,2,12,29,3,69,73,80,78,88,65,74,73,70,69,83,80,84,87,72,84,88,91,69,73,95,87,77,70,69,83,80,84,87,70,87,77,80,78,88,21,17,27,94,69,25,28,22,23,80,1,29,0,0,22,20,22,88,31,11,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,75,88,62,4,21,21,9,1,92,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,65,64,69,31,25,19,29,3,69,12,24,0,88,18,12,9,5,4,28,2,4,12,21,69,80,22,10,13,2,17,16,80,21,23,7,0,10,89,69,23,22,69,12,24,0,88,19,12,10,19,16,21,22,0,10,21,11,27,21,69,23,22,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,36,22,20,69,26,9,69,11,25,8,17,28,4,10,80,23,29,17,22,23,30,12,22,23,69,49,80,13,25,6,0,88,28,12,19,21,18,17,3,0,88,18,0,29,30,69,25,18,9,29,80,17,23,80,1,29,4,0,10,29,12,22,21,69,12,24,0,88,3,16,21,3,69,23,22,69,12,24,0,88,3,16,26,3,0,9,5,0,22,4,69,11,21,23,17,21,22,88,25,11,88,7,13,17,19,13,88,4,13,29,80,0,0,0,10,22,21,11,12,3,69,25,2,0,88,21,19,29,30,69,22,5,8,26,21,23,11,94

Diff for: project_euler/problem_059/sol1.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
Each character on a computer is assigned a unique code and the preferred standard is
3+
ASCII (American Standard Code for Information Interchange).
4+
For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.
5+
6+
A modern encryption method is to take a text file, convert the bytes to ASCII, then
7+
XOR each byte with a given value, taken from a secret key. The advantage with the
8+
XOR function is that using the same encryption key on the cipher text, restores
9+
the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
10+
11+
For unbreakable encryption, the key is the same length as the plain text message, and
12+
the key is made up of random bytes. The user would keep the encrypted message and the
13+
encryption key in different locations, and without both "halves", it is impossible to
14+
decrypt the message.
15+
16+
Unfortunately, this method is impractical for most users, so the modified method is
17+
to use a password as a key. If the password is shorter than the message, which is
18+
likely, the key is repeated cyclically throughout the message. The balance for this
19+
method is using a sufficiently long password key for security, but short enough to
20+
be memorable.
21+
22+
Your task has been made easy, as the encryption key consists of three lower case
23+
characters. Using p059_cipher.txt (right click and 'Save Link/Target As...'), a
24+
file containing the encrypted ASCII codes, and the knowledge that the plain text
25+
must contain common English words, decrypt the message and find the sum of the ASCII
26+
values in the original text.
27+
"""
28+
29+
30+
import string
31+
from itertools import cycle, product
32+
from pathlib import Path
33+
from typing import List, Optional, Set, Tuple
34+
35+
VALID_CHARS: str = (
36+
string.ascii_letters + string.digits + string.punctuation + string.whitespace
37+
)
38+
LOWERCASE_INTS: List[int] = [ord(letter) for letter in string.ascii_lowercase]
39+
VALID_INTS: Set[int] = {ord(char) for char in VALID_CHARS}
40+
41+
COMMON_WORDS: List[str] = ["the", "be", "to", "of", "and", "in", "that", "have"]
42+
43+
44+
def try_key(ciphertext: List[int], key: Tuple[int, ...]) -> Optional[str]:
45+
"""
46+
Given an encrypted message and a possible 3-character key, decrypt the message.
47+
If the decrypted message contains a invalid character, i.e. not an ASCII letter,
48+
a digit, punctuation or whitespace, then we know the key is incorrect, so return
49+
None.
50+
>>> try_key([0, 17, 20, 4, 27], (104, 116, 120))
51+
'hello'
52+
>>> try_key([68, 10, 300, 4, 27], (104, 116, 120)) is None
53+
True
54+
"""
55+
decoded: str = ""
56+
keychar: int
57+
cipherchar: int
58+
decodedchar: int
59+
60+
for keychar, cipherchar in zip(cycle(key), ciphertext):
61+
decodedchar = cipherchar ^ keychar
62+
if decodedchar not in VALID_INTS:
63+
return None
64+
decoded += chr(decodedchar)
65+
66+
return decoded
67+
68+
69+
def filter_valid_chars(ciphertext: List[int]) -> List[str]:
70+
"""
71+
Given an encrypted message, test all 3-character strings to try and find the
72+
key. Return a list of the possible decrypted messages.
73+
>>> from itertools import cycle
74+
>>> text = "The enemy's gate is down"
75+
>>> key = "end"
76+
>>> encoded = [ord(k) ^ ord(c) for k,c in zip(cycle(key), text)]
77+
>>> text in filter_valid_chars(encoded)
78+
True
79+
"""
80+
possibles: List[str] = []
81+
for key in product(LOWERCASE_INTS, repeat=3):
82+
encoded = try_key(ciphertext, key)
83+
if encoded is not None:
84+
possibles.append(encoded)
85+
return possibles
86+
87+
88+
def filter_common_word(possibles: List[str], common_word: str) -> List[str]:
89+
"""
90+
Given a list of possible decoded messages, narrow down the possibilities
91+
for checking for the presence of a specified common word. Only decoded messages
92+
containing common_word will be returned.
93+
>>> filter_common_word(['asfla adf', 'I am here', ' !?! #a'], 'am')
94+
['I am here']
95+
>>> filter_common_word(['athla amf', 'I am here', ' !?! #a'], 'am')
96+
['athla amf', 'I am here']
97+
"""
98+
return [possible for possible in possibles if common_word in possible.lower()]
99+
100+
101+
def solution(filename: str = "p059_cipher.txt") -> int:
102+
"""
103+
Test the ciphertext against all possible 3-character keys, then narrow down the
104+
possibilities by filtering using common words until there's only one possible
105+
decoded message.
106+
>>> solution("test_cipher.txt")
107+
3000
108+
"""
109+
ciphertext: List[int]
110+
possibles: List[str]
111+
common_word: str
112+
decoded_text: str
113+
data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8")
114+
115+
ciphertext = [int(number) for number in data.strip().split(",")]
116+
117+
possibles = filter_valid_chars(ciphertext)
118+
for common_word in COMMON_WORDS:
119+
possibles = filter_common_word(possibles, common_word)
120+
if len(possibles) == 1:
121+
break
122+
123+
decoded_text = possibles[0]
124+
return sum([ord(char) for char in decoded_text])
125+
126+
127+
if __name__ == "__main__":
128+
print(f"{solution() = }")

Diff for: project_euler/problem_059/test_cipher.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
63,13,28,75,0,23,14,8,0,76,22,89,12,4,13,14,69,16,24,69,29,4,18,23,69,69,59,14,69,11,14,4,29,18

0 commit comments

Comments
 (0)