|
| 1 | +from typing import List, Tuple |
| 2 | + |
| 3 | +from src.utils.data import load_data |
| 4 | +from src.utils.submission import submit_or_print |
| 5 | + |
| 6 | + |
| 7 | +def main(debug: bool) -> None: |
| 8 | + input_data = load_data(debug) |
| 9 | + |
| 10 | + keys, locks, height = parse_input(input_data) |
| 11 | + print(f"{len(locks)} locks") |
| 12 | + print(f"{len(keys)} keys") |
| 13 | + print(f"lock and key height: {height}") |
| 14 | + |
| 15 | + result_part1 = 0 |
| 16 | + for lock in locks: |
| 17 | + for key in keys: |
| 18 | + result_part1 += int(fit(key, lock, height)) |
| 19 | + |
| 20 | + result_part2 = None |
| 21 | + |
| 22 | + submit_or_print(result_part1, result_part2, debug) |
| 23 | + |
| 24 | + |
| 25 | +def parse_input(input_data: str) -> Tuple[List[List[int]], List[List[int]], int]: |
| 26 | + locks = [] |
| 27 | + keys = [] |
| 28 | + spl = input_data.split("\n\n") |
| 29 | + for s in spl: |
| 30 | + lines = s.splitlines() |
| 31 | + heights = [0 for _ in range(len(lines[0]))] |
| 32 | + for line in lines[1:-1]: |
| 33 | + for i, ch in enumerate(line): |
| 34 | + if "#" == ch: |
| 35 | + heights[i] += 1 |
| 36 | + |
| 37 | + lock = lines[0][0] == "#" |
| 38 | + if lock: |
| 39 | + locks.append(heights) |
| 40 | + else: |
| 41 | + keys.append(heights) |
| 42 | + |
| 43 | + height = len(spl[0].splitlines()) - 2 |
| 44 | + return keys, locks, height |
| 45 | + |
| 46 | + |
| 47 | +def fit(key: List[int], lock: List[int], height: int) -> bool: |
| 48 | + for l, k in zip(lock, key): |
| 49 | + if l + k > height: |
| 50 | + return False |
| 51 | + return True |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + debug_mode = True |
| 56 | + # debug_mode = False |
| 57 | + main(debug_mode) |
0 commit comments