From dcdecaec5635d97ff22aa99e8c6ee5c45318a1b9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 19 May 2025 00:58:08 +0100 Subject: [PATCH] 2016: d04: ex1: add solution --- 2016/d04/ex1/ex1.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 2016/d04/ex1/ex1.py diff --git a/2016/d04/ex1/ex1.py b/2016/d04/ex1/ex1.py new file mode 100755 index 0000000..5c50645 --- /dev/null +++ b/2016/d04/ex1/ex1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +import collections +import sys +from typing import NamedTuple + + +class Room(NamedTuple): + name: str + sector_id: int + checksum: str + + def is_real(self) -> bool: + letters = collections.Counter(self.name.replace("-", "")) + checksum = sorted(letters.most_common(), key=lambda t: (-t[1], t[0]))[:5] + return "".join(c for c, _ in checksum) == self.checksum + + @classmethod + def from_str(cls, input: str) -> "Room": + input, checksum = map(lambda s: s.removesuffix("]"), input.split("[")) + *name, sector_id = input.split("-") + return cls("-".join(name), int(sector_id), checksum) + + +def solve(input: str) -> int: + def parse(input: str) -> list[Room]: + return [Room.from_str(line) for line in input.splitlines()] + + rooms = parse(input) + return sum(room.sector_id for room in rooms if room.is_real()) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()