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 6525007 commit e4dfdc2Copy full SHA for e4dfdc2
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, start, *rest = nums
16
+ for ops in product(*[all_ops for _ in range(len(rest))]):
17
+ res = start
18
+ for num, op in zip(rest, 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