From a34941dbd92cdf3d319cb8f7c66e3d72801cf3b7 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()