Skip to content

Instantly share code, notes, and snippets.

@redspider
Created December 10, 2020 08:34
Show Gist options
  • Save redspider/fba01a6c81bb22aeca7b2f72b9bd90fc to your computer and use it in GitHub Desktop.
Save redspider/fba01a6c81bb22aeca7b2f72b9bd90fc to your computer and use it in GitHub Desktop.
adapters = [int(x) for x in INPUT.split("\n")]
# Part 1
last = 0
differences = []
for n in sorted(adapters):
differences.append(n - last)
last = n
differences.append(3)
print(differences.count(1) * differences.count(3))
# Part 2
G = nx.DiGraph()
G.add_node(max(adapters) + 3)
for n in [0] + adapters:
G.add_node(n)
for x in range(1, 4):
if n + x in adapters or n + x == max(adapters) + 3:
G.add_edge(n, n + x)
splits = []
for i, n in enumerate(sorted([0] + adapters)):
# A split is where there's only one path through
if n == 0:
splits.append(0)
if n - 2 not in adapters and n - 1 not in adapters and i > 2:
# have to go through this one
splits.append(n)
splits.append((max(adapters) + 3))
total = 1
start = 0
for end in splits[1:]:
foo = list(nx.all_simple_paths(G, start, end))
total *= len(foo)
start = end
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment