parent
a2d5923806
commit
6861227d39
@ -0,0 +1,45 @@
|
||||
import numpy as np
|
||||
from collections import deque
|
||||
|
||||
file = "inputs/day12.input"
|
||||
lines = open(file, "rb").read().decode().split("\n")[:-1]
|
||||
starts = []
|
||||
ends = []
|
||||
for line in lines:
|
||||
start, end = line.split("-")
|
||||
starts.append(start)
|
||||
ends.append(end)
|
||||
|
||||
nodes = set(starts + ends)
|
||||
neighbours = {key: [] for key in nodes}
|
||||
|
||||
for idx, start in enumerate(starts):
|
||||
neighbours[start].append(ends[idx])
|
||||
neighbours[ends[idx]].append(start)
|
||||
|
||||
|
||||
def find_way(node, way):
|
||||
if node == "end":
|
||||
way.append(node)
|
||||
ways.append(way)
|
||||
return
|
||||
adjacents = neighbours[node]
|
||||
if (
|
||||
len(adjacents) == 1
|
||||
and adjacents[0] == adjacents[0].lower()
|
||||
and adjacents[0] in way
|
||||
):
|
||||
return
|
||||
way.append(node)
|
||||
for adjacent in adjacents:
|
||||
if adjacent == adjacent.lower() and adjacent in way:
|
||||
continue
|
||||
else:
|
||||
find_way(adjacent, way.copy())
|
||||
|
||||
|
||||
ways = []
|
||||
find_way("start", [])
|
||||
print(neighbours)
|
||||
print(ways)
|
||||
print(len(ways))
|
@ -0,0 +1,10 @@
|
||||
dc-end
|
||||
HN-start
|
||||
start-kj
|
||||
dc-start
|
||||
dc-HN
|
||||
LN-dc
|
||||
HN-end
|
||||
kj-sa
|
||||
kj-HN
|
||||
kj-dc
|
Loading…
Reference in new issue