We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b47a9b6 commit c162e41Copy full SHA for c162e41
2024/day07/solutions.py
@@ -0,0 +1,30 @@
1
+from itertools import product
2
+from operator import add, mul
3
+import re
4
+
5
+with open("input") as f:
6
+ ns = [list(map(int, re.findall("\\d+", l))) for l in f.read().strip().split("\n")]
7
8
9
+def solve(part2):
10
+ worked = 0
11
+ all_ops = [mul, add]
12
+ if part2:
13
+ all_ops.append(lambda a, b: int(str(a) + str(b)))
14
+ for nums in ns:
15
+ test = nums[0]
16
+ for ops in list(product(*[all_ops for _ in range(len(nums) - 2)])):
17
+ res = nums[1]
18
+ for num, op in zip(nums[2:], ops):
19
+ res = op(res, num)
20
+ if res == test:
21
+ worked += res
22
+ break
23
+ return worked
24
25
26
+# Part 1
27
+print(solve(False))
28
29
+# Part 2
30
+print(solve(True))
0 commit comments