Skip to content

Commit

Permalink
Day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
aastrand committed Dec 25, 2023
1 parent ff917c5 commit cfa9892
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
55 changes: 55 additions & 0 deletions day25/day25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

import sys
from collections import defaultdict

from utils import io
from utils.graph import bfs, create_dotfile


def part1(filename, example=False):
graph = defaultdict(set)
for line in io.get_lines(filename):
node, neighours = line.split(": ")
for n in neighours.split(" "):
graph[node].add(n)
graph[n].add(node)

# visualize with neato: neato -Tpng graph.dot > graph.png; open graph.png
create_dotfile(graph)

# look at ^, these are
if example:
pairs = [
("hfx", "pzl"),
("cmg", "bvb"),
("jqt", "nvd"),
]
else:
pairs = [
("gzr", "qnz"),
("hgk", "pgz"),
("xgs", "lmj"),
]

for p1, p2 in pairs:
graph[p1].remove(p2)
graph[p2].remove(p1)

p = 1
candidates = set(graph.keys())
while candidates:
visited = bfs(candidates.pop(), graph)
p *= len(visited)
candidates -= visited

return p


def main():
assert part1("example.txt", True) == 54
print(part1("../input/2023/day25.txt"))


if __name__ == "__main__":
sys.exit(main())
13 changes: 13 additions & 0 deletions day25/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr
9 changes: 9 additions & 0 deletions utils/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,12 @@ def dfs(node, visited):
return max_length

return dfs(start, set())


def create_dotfile(graph):
with open("graph.dot", "w") as f:
f.write("digraph G {\n")
for node, neighours in graph.items():
for n in neighours:
f.write(f'"{node}" -> "{n}";\n')
f.write("}")

0 comments on commit cfa9892

Please sign in to comment.