parent
1d916e940a
commit
1ac455d7a1
3 changed files with 62 additions and 0 deletions
@ -0,0 +1,31 @@ |
||||
|
||||
with open("input") as file: |
||||
lines = file.read().splitlines() |
||||
edges = [set(line.split("-")) for line in lines] |
||||
|
||||
|
||||
def getAdjacent(knot: str) -> set[str]: |
||||
return {edge.difference({knot}).pop() for edge in edges if knot in edge} |
||||
|
||||
|
||||
def solve(): |
||||
def find(knot: str, visited: set[str], smallUsedTwice: bool) -> set[tuple[str, ...]]: |
||||
if knot == "end": |
||||
return {"end"} |
||||
foundPaths = set() |
||||
newVisited = visited.union({knot}) if knot.islower() else visited |
||||
for k in getAdjacent(knot).difference(visited): |
||||
if knot.islower() and not smallUsedTwice and knot != "start": |
||||
for p in find(k, visited, True): |
||||
foundPaths.add((knot, *p)) |
||||
for p in find(k, newVisited, smallUsedTwice): |
||||
foundPaths.add((knot, *p)) |
||||
return foundPaths |
||||
|
||||
paths1 = find("start", set(), True) |
||||
paths2 = find("start", set(), False) |
||||
|
||||
print(len(paths1), len(paths2)) |
||||
|
||||
|
||||
solve() |
@ -0,0 +1,24 @@ |
||||
EG-bj |
||||
LN-end |
||||
bj-LN |
||||
yv-start |
||||
iw-ch |
||||
ch-LN |
||||
EG-bn |
||||
OF-iw |
||||
LN-yv |
||||
iw-TQ |
||||
iw-start |
||||
TQ-ch |
||||
EG-end |
||||
bj-OF |
||||
OF-end |
||||
TQ-start |
||||
TQ-bj |
||||
iw-LN |
||||
EG-ch |
||||
yv-iw |
||||
KW-bj |
||||
OF-ch |
||||
bj-ch |
||||
yv-TQ |
@ -0,0 +1,7 @@ |
||||
start-A |
||||
start-b |
||||
A-c |
||||
A-b |
||||
b-d |
||||
A-end |
||||
b-end |
Loading…
Reference in new issue