day1: modify python for part2
This commit is contained in:
parent
b4004fd75f
commit
03abb1a5ae
1 changed files with 14 additions and 7 deletions
|
@ -1,15 +1,22 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
|
|
||||||
def find2020(nums: Iterable[int]) -> int:
|
def product(nums: Iterable[int]) -> int:
|
||||||
candidates = set()
|
"""
|
||||||
|
Returns the product of all elements in 'nums'
|
||||||
|
"""
|
||||||
|
x = 1
|
||||||
for num in nums:
|
for num in nums:
|
||||||
opposite = 2020-num
|
x *= num
|
||||||
if num in candidates:
|
return x
|
||||||
return num * opposite
|
|
||||||
candidates.add(opposite)
|
def find_summing(arity: int, nums: Iterable[int]) -> int:
|
||||||
|
return product(next(filter(lambda xs: sum(xs) == 2020, itertools.permutations(nums, r=arity))))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
with open(sys.argv[1], 'r') as f:
|
with open(sys.argv[1], 'r') as f:
|
||||||
print(find2020(int(n) for n in f.readlines()))
|
nums = [int(n) for n in f.readlines()]
|
||||||
|
print(find_summing(2, nums))
|
||||||
|
print(find_summing(3, nums))
|
||||||
|
|
Loading…
Reference in a new issue