-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·60 lines (49 loc) · 1.59 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
#!/usr/bin/env python3
import itertools
import utils
from year_2017.day_02 import part_a
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
250
"""
return SpreadsheetExtended.from_text(_input).get_even_checksum()
class SpreadsheetExtended(part_a.Spreadsheet):
def get_even_checksum(self):
"""
>>> SpreadsheetExtended.from_text((
... "5 9 2 8\\n"
... "9 4 7 3\\n"
... "3 8 6 5\\n"
... ).replace(' ', '\\t')).get_even_checksum()
9
"""
return sum(map(self.get_line_even_checksum, self.cells))
def get_line_even_checksum(self, line):
"""
>>> SpreadsheetExtended(()).get_line_even_checksum((5, 9, 2, 8))
4
"""
non_co_primes = self.get_non_co_primes(line)
if len(non_co_primes) != 2:
raise Exception(
f"Expected exactly 2 non-co-primes but got "
f"{len(non_co_primes)} in {line}")
smaller, larger = non_co_primes
return larger // smaller
def get_non_co_primes(self, line):
"""
>>> SpreadsheetExtended(()).get_non_co_primes((5, 9, 2, 8))
[2, 8]
>>> SpreadsheetExtended(()).get_non_co_primes((5, 9, 2, 8, 4))
[2, 4, 8]
"""
return sorted({
value
for smaller, larger in itertools.combinations(sorted(line), 2)
if larger % smaller == 0
for value in (smaller, larger)
})
Challenge.main()
challenge = Challenge()