blob: e3bd232e14a2c1ed49d4ce930e55b3acb0226846 (
plain) (
blame)
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
|
#!/usr/bin/python3
import dataclasses
import functools
import itertools
import operator
from typing import Self
@dataclasses.dataclass
class Machine:
target: int
buttons: list[int]
@classmethod
def from_record(cls, s: str) -> Self:
buttons: list[int] = []
for token in s.split(' '):
match token[0]:
case '[':
token = token.translate({
ord('.'): '0',
ord('#'): '1',
})
target = int(token[len(token) - 2:0:-1], base=2)
case '(':
n = 0
for x in map(int, token[1:-1].split(',')):
n |= 1 << x
buttons.append(n)
return cls(target, buttons)
def main() -> None:
with open('input', 'r') as f:
xs = [Machine.from_record(x) for x in f.readlines()]
print(sum(map(fewest_clicks, xs)))
def fewest_clicks(mach: Machine) -> int:
for i in itertools.count(start=1):
for comb in itertools.combinations(mach.buttons, i):
if functools.reduce(operator.xor, comb) == mach.target:
return i
# NOTREACHED
if __name__ == '__main__':
main()
|