From f157debb7b84f4876e6eba92253b48bb4aaf668b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 13 Dec 2020 07:38:51 +0100 Subject: [PATCH] 2020: d13: ex1: add solution --- 2020/d13/ex1/ex1.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 2020/d13/ex1/ex1.py diff --git a/2020/d13/ex1/ex1.py b/2020/d13/ex1/ex1.py new file mode 100755 index 0000000..fadaefb --- /dev/null +++ b/2020/d13/ex1/ex1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import itertools +import math +import sys +from typing import List, Tuple + + +def find_first_factor(earliest: int, timings: List[int]) -> Tuple[int, int]: + timings = sorted(timings) + for t in itertools.count(earliest): + for n in timings: + if t % n == 0: + return t - earliest, n + + assert False # Make Mypy happy + + +def solve(raw: List[str]) -> int: + return math.prod( + find_first_factor(int(raw[0]), [int(i) for i in raw[1].split(",") if i != "x"]) + ) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()