From 721750965b37f903a52525cafa90f180b1a0d0ad Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 3 Dec 2020 18:22:09 +0100 Subject: [PATCH] 2020: d03: ex2: add solution --- 2020/d03/ex2/ex2.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 2020/d03/ex2/ex2.py diff --git a/2020/d03/ex2/ex2.py b/2020/d03/ex2/ex2.py new file mode 100755 index 0000000..c061deb --- /dev/null +++ b/2020/d03/ex2/ex2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import sys +from math import prod +from typing import List, Tuple + + +def solve(trees: List[str], delta: Tuple[int, int]) -> int: + x, y = 0, 0 + sum = 0 + while True: + x += delta[0] + y += delta[1] + if y >= len(trees): + break + sum += trees[y][x % len(trees[0])] == "#" + return sum + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + deltas = [ + (1, 1), + (3, 1), + (5, 1), + (7, 1), + (1, 2), + ] + print(prod(solve(input, delt) for delt in deltas)) + + +if __name__ == "__main__": + main()