-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·68 lines (59 loc) · 2.13 KB
/
part_b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
from collections import Iterable
from utils import BaseChallenge, helper
from . import part_a
class Challenge(BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
'lragovly'
"""
return DecoderExtended.from_messages_text(_input).decode()
class DecoderExtended(part_a.Decoder):
"""
>>> DecoderExtended.from_messages_text(
... "eedadn\\ndrvtee\\neandsr\\nraavrd\\natevrs\\ntsrnev\\n"
... "sdttsa\\nrasrtv\\nnssdts\\nntnada\\nsvetve\\ntesnvt\\n"
... "vntsnd\\nvrdear\\ndvrsen\\nenarar").decode()
'advent'
"""
def decode_column(self, position: int) -> str:
"""
>>> DecoderExtended(
... messages=['eedadn', 'drvtee', 'eandsr']).decode_column(0)
'd'
>>> DecoderExtended(
... messages=['eedadn', 'drvtee', 'eandsr']).decode_column(5)
Traceback (most recent call last):
...
Exception: Too many letters (3) were the least common
"""
column = self.get_column(position)
return self.get_least_common_letter(column)
def get_least_common_letter(self, letters: Iterable[str]) -> str:
"""
>>> DecoderExtended([]).get_least_common_letter('aaabbccd')
'd'
>>> DecoderExtended([]).get_least_common_letter('aaabbbccdd')
Traceback (most recent call last):
...
Exception: Too many letters (2) were the least common
"""
letter_counts = {
letter: len(items)
for letter, items in helper.group_by(letters).items()
}
least_common_count = min(letter_counts.values())
least_common_letters = [
letter
for letter, count in letter_counts.items()
if count == least_common_count
]
if len(least_common_letters) > 1:
raise Exception(
f"Too many letters ({len(least_common_letters)}) were the "
f"least common")
least_common_letter, = least_common_letters
return least_common_letter
Challenge.main()
challenge = Challenge()