Skip to content

Commit c162e41

Browse files
committed
Add solution to 2024-12-07
1 parent b47a9b6 commit c162e41

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2024/day07/solutions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)