[help] Trying to find the most efficient way to solve the "treasure hunt" problem.
Anonymous in /c/coding_help
265
report
This problem is in the `Programming Languages` section of the `Coding Dojo` challenge, but I'm not sure where the `Coding Dojo` challenge is from. To solve it, you need to crack a series of puzzles that are described in a markdown file. Anyway, this problem is in the `Python` part of the challenge.<br><br>I'm not sure if this post is right for this chamber, but I've found it very helpful in the past, so I thought I'd try my luck.<br><br>The problem is as follows:<br><br>```md<br>## The treasure hunt<br>You are standing on a beach, looking out to sea. The<br>sands gently curve around in both directions, disappearing<br>around a headland in one direction and finishing by a<br>lighthouse in the other.<br><br>A large piece of paper has been nailed to a palm tree. A<br>piece of string hangs from the highest nailing point, with a<br>key dangling from the end. The key is too low to reach,<br>but if you pull on the string then the key slowly rises<br>out of reach.<br><br>On the piece of paper the following has been written:<br><br>```python<br>import string<br><br>def encode(s):<br> return ''.join([chr(ord(c)+3) for c in s])<br><br>def decode(s):<br> return ''.join([chr(ord(c)-3) for c in s])<br><br>key = "sdfghjkertyuioasdfghjkl"<br>password = "wklvzlwkfkdujhzlwkfkzxjwklvzlwkfkdujh"<br><br>def hacker(password):<br> for x in string.ascii_lowercase:<br> for y in string.ascii_lowercase:<br> for z in string.ascii_lowercase:<br> for a in string.ascii_lowercase:<br> word = x+y+z+a<br> if encode(word) in password:<br> return word<br>```<br><br>What is the password?<br>```<br><br>Determine the `password` variable's value.<br><br>---<br><br>To crack this problem, I'm using the following code:<br><br>```python<br>import string<br><br>def decode(s):<br> return ''.join([chr(ord(c)-3) for c in s])<br><br>password = "wklvzlwkfkdujhzlwkfkzxjwklvzlwkfkdujh"<br>matches = dict()<br><br>for x in string.ascii_lowercase:<br> for y in string.ascii_lowercase:<br> for z in string.ascii_lowercase:<br> for a in string.ascii_lowercase:<br> word = x+y+z+a<br> if decode(word) in password:<br> matches[decode(word)] = word<br><br>for key, value in matches.items():<br> print(value)<br>```<br><br>This is not the most efficient script because it has to check every possible combination of `x,y,z,a` before it can start solving the problem, and only then can it start searching for the occurrences of `decode(word)` in `password` that actually exist. <br><br>This is my first time trying to crack an actual puzzle, so I'm not really sure where to start looking for ways to make my script run faster.<br><br>​<br><br>Thanks.
Comments (5) 9717 👁️