add 2024/day25.1
This commit is contained in:
parent
e77aa81bc0
commit
46a7e459f6
1 changed files with 79 additions and 0 deletions
79
2024/25/main.py
Normal file
79
2024/25/main.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue