Skip to content

Commit

Permalink
Add solution to 2024-12-07
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 7, 2024
1 parent b47a9b6 commit c162e41
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 2024/day07/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from itertools import product
from operator import add, mul
import re

with open("input") as f:
ns = [list(map(int, re.findall("\\d+", l))) for l in f.read().strip().split("\n")]


def solve(part2):
worked = 0
all_ops = [mul, add]
if part2:
all_ops.append(lambda a, b: int(str(a) + str(b)))
for nums in ns:
test = nums[0]
for ops in list(product(*[all_ops for _ in range(len(nums) - 2)])):
res = nums[1]
for num, op in zip(nums[2:], ops):
res = op(res, num)
if res == test:
worked += res
break
return worked


# Part 1
print(solve(False))

# Part 2
print(solve(True))

0 comments on commit c162e41

Please sign in to comment.