Returns the shortest possible secret passcode of unknown length. >>> 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" "26
(logins: list[str])
| 19 | |
| 20 | |
| 21 | def find_secret_passcode(logins: list[str]) -> int: |
| 22 | """ |
| 23 | Returns the shortest possible secret passcode of unknown length. |
| 24 | |
| 25 | >>> find_secret_passcode(["135", "259", "235", "189", "690", "168", "120", |
| 26 | ... "136", "289", "589", "160", "165", "580", "369", "250", "280"]) |
| 27 | 12365890 |
| 28 | |
| 29 | >>> find_secret_passcode(["426", "281", "061", "819" "268", "406", "420", |
| 30 | ... "428", "209", "689", "019", "421", "469", "261", "681", "201"]) |
| 31 | 4206819 |
| 32 | """ |
| 33 | |
| 34 | # Split each login by character e.g. '319' -> ('3', '1', '9') |
| 35 | split_logins = [tuple(login) for login in logins] |
| 36 | |
| 37 | unique_chars = {char for login in split_logins for char in login} |
| 38 | |
| 39 | for permutation in itertools.permutations(unique_chars): |
| 40 | satisfied = True |
| 41 | for login in logins: |
| 42 | if not ( |
| 43 | permutation.index(login[0]) |
| 44 | < permutation.index(login[1]) |
| 45 | < permutation.index(login[2]) |
| 46 | ): |
| 47 | satisfied = False |
| 48 | break |
| 49 | |
| 50 | if satisfied: |
| 51 | return int("".join(permutation)) |
| 52 | |
| 53 | raise Exception("Unable to find the secret passcode") |
| 54 | |
| 55 | |
| 56 | def solution(input_file: str = "keylog.txt") -> int: |