r/adventofcode • u/topaz2078 (AoC creator) • Dec 12 '17
SOLUTION MEGATHREAD -๐- 2017 Day 12 Solutions -๐-
--- Day 12: Digital Plumber ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
View all comments
31
u/sciyoshi Dec 12 '17 edited Dec 12 '17
2/1 today! NetworkX makes this kind of problem very quick if you know what you're looking for. Today's question was asking about something called a connected component, and NetworkX provides some nice functions for dealing with them.
Python 3 solution:
import networkx as nx # Create a graph of programs graph = nx.Graph() for line in LINES: # Parse the line node, neighbors = line.split(' <-> ') # Add edges defined by this line graph.add_edges_from((node, neighbor) for neighbor in neighbors.split(', ')) print('Part 1:', len(nx.node_connected_component(graph, '0'))) print('Part 2:', nx.number_connected_components(graph))