From 46a7e459f67aefde03b6f9f0a5305cd963437870 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 25 Dec 2024 11:23:57 +0100 Subject: [PATCH] add 2024/day25.1 --- 2024/25/main.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 2024/25/main.py diff --git a/2024/25/main.py b/2024/25/main.py new file mode 100644 index 0000000..63e428c --- /dev/null +++ b/2024/25/main.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + +import re + + +def parse_lock(block: list) -> list: + lock = [0] * 5 + for y, _ in enumerate(block): + pins = re.finditer(r"#", block[y]) + for p in pins: + lock[p.start()] = y + + return lock + + +def parse_key(block: list) -> list: + key = [0] * 5 + for y, _ in enumerate(block): + pins = re.finditer(r"\.", block[y]) + for p in pins: + key[p.start()] = len(block) - 2 - y + + return key + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + schematics = [] + current_schematic = [] + + for line in file: + line = line.rstrip() + if line: + current_schematic.append(line) + else: + schematics.append(current_schematic) + current_schematic = [] + + schematics.append(current_schematic) + + keys = [] + locks = [] + for schematic in schematics: + if schematic[0] == "#####": + locks.append(parse_lock(schematic)) + else: + keys.append(parse_key(schematic)) + + return keys, locks + + +def key_fits_lock(key: list, lock: list) -> bool: + for k, l in zip(key, lock): + if k + l >= 6: + return False + return True + + +def main(): + keys, locks = readinput() + + # part 1 + fitting = 0 + for key in keys: + for lock in locks: + if key_fits_lock(key, lock): + fitting += 1 + + print("Fitting keys: %d" % fitting) + + +if __name__ == "__main__": + main()