1
0
Fork 0
adventofcode/2024/25/main.py

80 lines
1.7 KiB
Python
Raw Normal View History

2024-12-25 10:23:57 +00:00
#!/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()