Skip to content

Instantly share code, notes, and snippets.

@rodrigogiraoserrao
Created December 27, 2024 20:07
Show Gist options
  • Save rodrigogiraoserrao/00924badb7d2552e588c9a6b952d448e to your computer and use it in GitHub Desktop.
Save rodrigogiraoserrao/00924badb7d2552e588c9a6b952d448e to your computer and use it in GitHub Desktop.
# === Parsing ===
from collections import defaultdict
edges = defaultdict(set)
with open("input.txt", "r") as f:
for line in f:
n1, n2 = line.strip().split("-")
edges[n1].add(n2)
edges[n2].add(n1)
# === Part 1 ===
connected = set()
for n1, neighbours in edges.items():
for n2 in neighbours:
for n3 in neighbours & edges[n2]:
connected.add(tuple(sorted((n1, n2, n3))))
count = 0
for group in connected:
count += any(node.startswith("t") for node in group)
count = sum(any(node.startswith("t") for node in group) for group in connected)
print(count)
# === Part 2 ===
def get_clicks(click, nodes, neighbours):
# Try to expand the click.
for idx, node in enumerate(nodes):
if click <= neighbours[node]:
yield from get_clicks(click | {node}, nodes[idx:], neighbours)
yield click
largest_click = max(get_clicks(set(), list(edges.keys()), edges), key=len)
print(",".join(sorted(largest_click)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment