From 5da7942d780b94b789ad88afede090e553b74d5d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 9 Dec 2020 09:10:46 +0100 Subject: [PATCH] 2020: d09: ex1: add solution --- 2020/d09/ex1/ex1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 2020/d09/ex1/ex1.py diff --git a/2020/d09/ex1/ex1.py b/2020/d09/ex1/ex1.py new file mode 100755 index 0000000..318e38f --- /dev/null +++ b/2020/d09/ex1/ex1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + + +import itertools +import sys +from typing import List + + +def find_invalid(nums: List[int]) -> int: + for i in range(25, len(nums)): + num = nums[i] + found = False + for lhs, rhs in itertools.combinations(nums[i - 25 : i], 2): + if lhs + rhs == num: + found = True + break + if not found: + return num + assert False # Sanity check + + +def solve(raw: List[str]) -> int: + return find_invalid([int(line) for line in raw]) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()