-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Project Euler problem 79 solution 1 #8607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dhruvmanila
merged 5 commits into
TheAlgorithms:master
from
ishabahmed:project_euler_079
Apr 2, 2023
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2fe777
Add Project Euler problem 79 solution 1
ishabahmed 4a1f730
Updated to follow repo standards
ishabahmed 716ff0c
Fixed test cases
ishabahmed 9466b10
Remove `doctest` invocation in main block
dhruvmanila e982f92
Use `pathlib`, add `solution` doctest
dhruvmanila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
319 | ||
680 | ||
180 | ||
690 | ||
129 | ||
620 | ||
762 | ||
689 | ||
762 | ||
318 | ||
368 | ||
710 | ||
720 | ||
710 | ||
629 | ||
168 | ||
160 | ||
689 | ||
716 | ||
731 | ||
736 | ||
729 | ||
316 | ||
729 | ||
729 | ||
710 | ||
769 | ||
290 | ||
719 | ||
680 | ||
318 | ||
389 | ||
162 | ||
289 | ||
162 | ||
718 | ||
729 | ||
319 | ||
790 | ||
680 | ||
890 | ||
362 | ||
319 | ||
760 | ||
316 | ||
729 | ||
380 | ||
319 | ||
728 | ||
716 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Project Euler Problem 79: https://projecteuler.net/problem=79 | ||
|
||
Passcode derivation | ||
|
||
A common security method used for online banking is to ask the user for three | ||
random characters from a passcode. For example, if the passcode was 531278, | ||
they may ask for the 2nd, 3rd, and 5th characters; the expected reply would | ||
be: 317. | ||
|
||
The text file, keylog.txt, contains fifty successful login attempts. | ||
|
||
Given that the three characters are always asked for in order, analyse the file | ||
so as to determine the shortest possible secret passcode of unknown length. | ||
""" | ||
import itertools | ||
import os | ||
|
||
|
||
def find_secret_passcode(logins: list[str]) -> int: | ||
""" | ||
Returns the shortest possible secret passcode of unknown length. | ||
|
||
>>> find_secret_passcode(["319", "680", "180", "690", "129", "620", "698", | ||
... "318", "328", "310", "320", "610", "629", "198", "190", "631"]) | ||
6312980 | ||
|
||
>>> find_secret_passcode(["135", "259", "235", "189", "690", "168", "120", | ||
... "136", "289", "589", "160", "165", "580", "369", "250", "280"]) | ||
12365890 | ||
|
||
>>> find_secret_passcode(["426", "281", "061", "819" "268", "406", "420", | ||
... "428", "209", "689", "019", "421", "469", "261", "681", "201"]) | ||
4206819 | ||
""" | ||
|
||
# Split each login by character e.g. '319' -> ('3', '1', '9') | ||
split_logins = [tuple(login) for login in logins] | ||
|
||
unique_chars = {char for login in split_logins for char in login} | ||
|
||
for permutation in itertools.permutations(unique_chars): | ||
satisfied = True | ||
for login in logins: | ||
if not ( | ||
permutation.index(login[0]) | ||
< permutation.index(login[1]) | ||
< permutation.index(login[2]) | ||
): | ||
satisfied = False | ||
break | ||
|
||
if satisfied: | ||
return int("".join(permutation)) | ||
break | ||
|
||
raise Exception("Unable to find the secret passcode") | ||
|
||
|
||
def solution() -> int: | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Returns the shortest possible secret passcode of unknown length | ||
for successful login attempts given by keylog.txt. | ||
""" | ||
with open(os.path.dirname(__file__) + "/keylog.txt") as file: | ||
logins = file.read().splitlines() | ||
|
||
return find_secret_passcode(logins) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.