2024 Dec 07th
This commit is contained in:
parent
5b01fc0fbf
commit
d6b3b26f63
1 changed files with 22 additions and 0 deletions
22
Python/2024/07/main.py
Normal file
22
Python/2024/07/main.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
input = open("input", 'r')
|
||||||
|
|
||||||
|
d = []
|
||||||
|
for line in input:
|
||||||
|
ans, nums = line.split(": ")
|
||||||
|
d.append((int(ans), [int(num) for num in nums.split(" ")]))
|
||||||
|
|
||||||
|
def run(ans, eq, p2=False):
|
||||||
|
if len(eq) == 1:
|
||||||
|
return ans == eq[0]
|
||||||
|
ops = {lambda x,y: (x*y), lambda x,y: (x+y)}
|
||||||
|
if p2:
|
||||||
|
ops.add(lambda x,y: int(str(x) + str(y)))
|
||||||
|
|
||||||
|
x,y,*eqs = eq
|
||||||
|
for op in ops:
|
||||||
|
if run(ans, [op(x,y)] + eqs, p2):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
print(sum([ans for ans, eq in d if run(ans,eq)]))
|
||||||
|
print(sum([ans for ans, eq in d if run(ans,eq,True)]))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue