From 7eeab208b0d35ce2e66ea64d9999f2e365b871ce Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 29 Dec 2024 14:43:26 -0500 Subject: [PATCH] 2018: d02: ex1: add solution --- 2018/d02/ex1/ex1.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 2018/d02/ex1/ex1.py diff --git a/2018/d02/ex1/ex1.py b/2018/d02/ex1/ex1.py new file mode 100755 index 0000000..db848be --- /dev/null +++ b/2018/d02/ex1/ex1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +import sys +from collections import Counter + + +def solve(input: str) -> int: + def any_letter(boxes: list[str], count: int) -> int: + return sum(count in Counter(box).values() for box in boxes) + + def checksum(boxes: list[str]) -> int: + return any_letter(boxes, 2) * any_letter(boxes, 3) + + boxes = input.splitlines() + return checksum(boxes) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()