aboutsummaryrefslogtreecommitdiff
path: root/2025/10/puzzle-1.py
diff options
context:
space:
mode:
Diffstat (limited to '2025/10/puzzle-1.py')
-rwxr-xr-x2025/10/puzzle-1.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/2025/10/puzzle-1.py b/2025/10/puzzle-1.py
new file mode 100755
index 0000000..e3bd232
--- /dev/null
+++ b/2025/10/puzzle-1.py
@@ -0,0 +1,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()