Test the ciphertext against all possible 3-character keys, then narrow down the possibilities by filtering using common words until there's only one possible decoded message. >>> solution("test_cipher.txt") 3000
(filename: str = "p059_cipher.txt")
| 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__": |
no test coverage detected