Compare commits

...

4 commits

4 changed files with 6863 additions and 0 deletions

42
2024/d23/ex1/ex1.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import sys
from collections import defaultdict
def solve(input: str) -> int:
def parse_line(input: str) -> tuple[str, str]:
lhs, rhs = input.split("-")
return lhs, rhs
def parse(input: list[str]) -> list[tuple[str, str]]:
return [parse_line(line) for line in input]
def links_to_graph(topology: list[tuple[str, str]]) -> dict[str, set[str]]:
graph: dict[str, set[str]] = defaultdict(set)
for lhs, rhs in topology:
graph[lhs].add(rhs)
graph[rhs].add(lhs)
return graph
def find_three_groups(graph: dict[str, set[str]]) -> set[tuple[str, str, str]]:
res: set[tuple[str, str, str]] = set()
for node in graph:
for neighbour in graph[node]:
for third in graph[node] & graph[neighbour]:
res.add(tuple(sorted((node, neighbour, third)))) # type: ignore
return res
topology = parse(input.splitlines())
graph = links_to_graph(topology)
groups = find_three_groups(graph)
return sum(any(computer.startswith("t") for computer in group) for group in groups)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

3380
2024/d23/ex1/input Normal file

File diff suppressed because it is too large Load diff

61
2024/d23/ex2/ex2.py Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
import sys
from collections import defaultdict
def solve(input: str) -> str:
def parse_line(input: str) -> tuple[str, str]:
lhs, rhs = input.split("-")
return lhs, rhs
def parse(input: list[str]) -> list[tuple[str, str]]:
return [parse_line(line) for line in input]
def links_to_graph(topology: list[tuple[str, str]]) -> dict[str, set[str]]:
graph: dict[str, set[str]] = defaultdict(set)
for lhs, rhs in topology:
graph[lhs].add(rhs)
graph[rhs].add(lhs)
return graph
# Maximum clique solution thanks to [1]
# [1]: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm
def bron_kerbosch(graph: dict[str, set[str]]) -> list[set[str]]:
# Use `output` as an output parameter
def helper(
clique: set[str],
candidates: set[str],
discarded: set[str],
output: list[set],
) -> None:
if not candidates and not discarded:
output.append(clique)
while candidates:
v = candidates.pop()
helper(
clique | {v},
candidates & graph[v],
discarded & graph[v],
output,
)
discarded.add(v)
cliques: list[set] = []
helper(set(), set(graph.keys()), set(), cliques)
return cliques
topology = parse(input.splitlines())
graph = links_to_graph(topology)
cliques = bron_kerbosch(graph)
historian_group = max(cliques, key=len) # MyPy doesn't like it inline in `sorted`
return ",".join(sorted(historian_group))
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

3380
2024/d23/ex2/input Normal file

File diff suppressed because it is too large Load diff