From 64a9d16216eea7125d03b60e31372c5e38f797d7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 4 Dec 2022 06:31:58 +0100 Subject: [PATCH] 2022: d04: ex2: add solution --- 2022/d04/ex2/ex2.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 2022/d04/ex2/ex2.py diff --git a/2022/d04/ex2/ex2.py b/2022/d04/ex2/ex2.py new file mode 100755 index 0000000..1b0049c --- /dev/null +++ b/2022/d04/ex2/ex2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: list[str]) -> int: + def is_overlapping(line: str) -> bool: + def assignment_to_set(elf: str) -> set[int]: + start, end = elf.split("-") + return set(range(int(start), int(end) + 1)) + + elf1, elf2 = map(assignment_to_set, line.split(",")) + return bool(elf1 & elf2) + + return sum(map(is_overlapping, input)) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()