2024 - Day 25 (Code Chronicle)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3

#Advent of Code
#2024 Day 25, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input: list[str]) -> int:
    locks: set[tuple[int, int, int, int, int]] = set()
    keys: set[tuple[int, int, int, int, int]] = set()

    rows: list[str] = []
    for line in puzzle_input + [""]: # Add extra empty line at the end to ensure the final key is parsed (it's a bit flaky, but hey. It's Christmas.)
        if not line:
            if rows[0] == "#" * 5:
                locks.add(tuple(col.count("#") - 1 for col in zip(*rows)))
            else:
                keys.add(tuple(col.count("#") - 1 for col in zip(*rows)))
            rows = []
        else:
            rows.append(line)

    return sum(1 for lock in locks for key in keys if all(lock_col + key_col < 6 for lock_col, key_col in zip(lock, key)))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The number of unique lock/key pairs that fit together is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["#####",
                                       ".####",
                                       ".####",
                                       ".####",
                                       ".#.#.",
                                       ".#...",
                                       ".....",
                                       "",
                                       "#####",
                                       "##.##",
                                       ".#.##",
                                       "...##",
                                       "...#.",
                                       "...#.",
                                       ".....",
                                       "",
                                       ".....",
                                       "#....",
                                       "#....",
                                       "#...#",
                                       "#.#.#",
                                       "#.###",
                                       "#####",
                                       "",
                                       ".....",
                                       ".....",
                                       "#.#..",
                                       "###..",
                                       "###.#",
                                       "###.#",
                                       "#####",
                                       "",
                                       ".....",
                                       ".....",
                                       ".....",
                                       "#....",
                                       "#.#..",
                                       "#.#.#",
                                       "#####"]), 3)

if __name__ == "__main__":
    run(main)
Return to solution index