advent-of-code/2020/day1/day1.py

23 lines
585 B
Python
Raw Normal View History

2020-12-01 16:40:10 +01:00
#!/usr/bin/env python
2020-12-01 16:50:45 +01:00
import itertools
2020-12-01 16:40:10 +01:00
import sys
from typing import Iterable
2020-12-01 16:50:45 +01:00
def product(nums: Iterable[int]) -> int:
"""
Returns the product of all elements in 'nums'
"""
x = 1
2020-12-01 16:40:10 +01:00
for num in nums:
2020-12-01 16:50:45 +01:00
x *= num
return x
def find_summing(arity: int, nums: Iterable[int]) -> int:
return product(next(filter(lambda xs: sum(xs) == 2020, itertools.permutations(nums, r=arity))))
2020-12-01 16:40:10 +01:00
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
2020-12-01 16:50:45 +01:00
nums = [int(n) for n in f.readlines()]
print(find_summing(2, nums))
print(find_summing(3, nums))