From a268ab8be0952be7cb6ab44827998a123c631fec Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 8 Dec 2021 10:57:12 +0100 Subject: [PATCH] 2021: d08: ex1: add solution --- 2021/d08/ex1/ex1.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 2021/d08/ex1/ex1.py diff --git a/2021/d08/ex1/ex1.py b/2021/d08/ex1/ex1.py new file mode 100755 index 0000000..86716b0 --- /dev/null +++ b/2021/d08/ex1/ex1.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import itertools +import sys +from dataclasses import dataclass +from typing import List, Set, Tuple + + +@dataclass +class Entry: + signals: List[Set[str]] + outputs: List[Set[str]] + + +def solve(input: List[str]) -> int: + def parse_entry(input: str) -> Entry: + signals, outputs = input.split(" | ") + return Entry( + [set(s) for s in signals.split()], [set(o) for o in outputs.split()] + ) + + entries = [parse_entry(line) for line in input] + + return sum( + itertools.chain.from_iterable( + map(lambda __: 1, filter(lambda d: len(d) in (2, 3, 4, 7), entry.outputs)) + for entry in entries + ) + ) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()