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.
57 lines
1.1 KiB
57 lines
1.1 KiB
4 years ago
|
import pytest
|
||
|
from itertools import combinations
|
||
|
from typing import Tuple, List
|
||
|
from math import prod
|
||
|
|
||
|
|
||
|
def test_find_sum_two():
|
||
|
test_string = """
|
||
|
1721
|
||
|
979
|
||
|
366
|
||
|
299
|
||
|
675
|
||
|
1456
|
||
|
"""
|
||
|
assert find_sum(intify_input(test_string), 2) == (1721, 299)
|
||
|
|
||
|
|
||
|
def test_find_sum_three():
|
||
|
test_string = """
|
||
|
1721
|
||
|
979
|
||
|
366
|
||
|
299
|
||
|
675
|
||
|
145"""
|
||
|
assert find_sum(intify_input(test_string), 3) == (979, 366, 675)
|
||
|
|
||
|
|
||
|
def read_input_data(path: str) -> str:
|
||
|
with open(path, 'r') as f:
|
||
|
data = f.read()
|
||
|
return data
|
||
|
|
||
|
|
||
|
def find_sum(data: List[int], n_summands: int = 2, target: int = 2020) -> Tuple[int, int]:
|
||
|
for combo in combinations(data, n_summands):
|
||
|
if sum(combo) == target:
|
||
|
print(f'sum({combo}) == {target}')
|
||
|
return combo
|
||
|
|
||
|
|
||
|
def produce_result(data_path: str, n_summands) -> int:
|
||
|
data = intify_input(read_input_data(data_path))
|
||
|
combo = find_sum(data, n_summands)
|
||
|
return prod(combo)
|
||
|
|
||
|
|
||
|
def intify_input(data):
|
||
|
data = [int(num) for num in data.split('\n') if num]
|
||
|
return data
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
result = produce_result('input.txt', 3)
|
||
|
print(f'Result: {result}')
|