-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters