-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |