You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
876 B
31 lines
876 B
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
def maths(nums, sec=False):
|
|
if len(nums) == 2:
|
|
yield nums[0] * nums[1]
|
|
yield nums[0] + nums[1]
|
|
if sec: yield int(str(nums[0]) + str(nums[1]))
|
|
else:
|
|
yield from maths([nums[0] * nums[1]] + nums[2:], sec)
|
|
yield from maths([nums[0] + nums[1]] + nums[2:], sec)
|
|
if sec: yield from maths([int(str(nums[0]) + str(nums[1]))] + nums[2:], sec)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
lines = [line.strip('\n') for line in open(sys.argv[1])]
|
|
res1, res2 = 0, 0
|
|
for line in lines:
|
|
test, nums = line.split(': ')
|
|
nums = [int(n) for n in nums.split(' ')]
|
|
if int(test) in maths(nums): res1 += int(test)
|
|
if int(test) in maths(nums, True): res2 += int(test)
|
|
|
|
# challenge 1
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|