From 679a7ab896edcabe24f09bfbfefb6ec0504077b2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 9 May 2025 23:36:59 +0100 Subject: [PATCH] 2017: d13: ex1: add solution --- 2017/d13/ex1/ex1.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 2017/d13/ex1/ex1.py diff --git a/2017/d13/ex1/ex1.py b/2017/d13/ex1/ex1.py new file mode 100755 index 0000000..3c23665 --- /dev/null +++ b/2017/d13/ex1/ex1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: str) -> int: + def parse(input: str) -> dict[int, int]: + return { + depth: range + for depth, range in (map(int, l.split(": ")) for l in input.splitlines()) + } + + def would_catch(depth: int, range: int) -> bool: + cycle_length = (range - 1) * 2 + return depth % cycle_length == 0 + + def compute_severity(firewall: dict[int, int]) -> int: + return sum( + depth * range + for depth, range in firewall.items() + if would_catch(depth, range) + ) + + firewall = parse(input) + return compute_severity(firewall) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()