diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-24 17:56:50 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-24 17:56:50 +0100 |
commit | 14175103cc4e90b92a051bd37b2168365b7574db (patch) | |
tree | 2c6f0f85c4808d6902c247f1453d267a0f44ef95 | |
parent | 3774cc95387d2d334de0dd192e611b548f0989aa (diff) |
Add 2024 day 23 solutions
-rw-r--r-- | 2024/23/.gitignore | 1 | ||||
-rw-r--r-- | 2024/23/Makefile | 1 | ||||
-rw-r--r-- | 2024/23/puzzles.py | 55 |
3 files changed, 57 insertions, 0 deletions
diff --git a/2024/23/.gitignore b/2024/23/.gitignore new file mode 100644 index 0000000..8931d44 --- /dev/null +++ b/2024/23/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py
\ No newline at end of file diff --git a/2024/23/Makefile b/2024/23/Makefile new file mode 100644 index 0000000..6e58a9d --- /dev/null +++ b/2024/23/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/py.mk
\ No newline at end of file diff --git a/2024/23/puzzles.py b/2024/23/puzzles.py new file mode 100644 index 0000000..93fdc0f --- /dev/null +++ b/2024/23/puzzles.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 + +import collections +import itertools + + +def main() -> None: + g = collections.defaultdict(set) + with open("input", "r") as f: + for line in f.readlines(): + l, r = line.rstrip().split("-") + g[l].add(r) + g[r].add(l) + + # START PART 1 + cliques: set[tuple[str, str, str]] = set() + # END PART 1 START PART 2 + minsz = 1 + clique: list[str] = [] + # END PART 2 + + for x, s in g.items(): + # START PART 2 + for i in range(minsz + 1, len(s) + 1): + # END PART 2 START PART 1 + i = 3 + # END PART 1 + nodes = tuple(s) + (x,) + for comb in itertools.combinations(nodes, i): + if cliquep(g, comb): + # START PART 1 + if any(x[0] == 't' for x in comb): + cliques.add(tuple(sorted(comb))) + # END PART 1 START PART 2 + minsz = i + clique = sorted(comb) + # END PART 2 + + # START PART 1 + print(len(cliques)) + # END PART 1 START PART 2 + print(','.join(clique)) + # END PART 2 + + +def cliquep(g: dict[str, set[str]], nodes: tuple[str, ...]) -> bool: + for i, x in enumerate(nodes): + for y in nodes[i + 1:]: + if x not in g[y]: + return False + return True + + +if __name__ == "__main__": + main()
\ No newline at end of file |