Skip to content

Instantly share code, notes, and snippets.

@Mu-adventofcode
Created May 6, 2022 18:17
Show Gist options
  • Save Mu-adventofcode/373d42ff22236f9dcbc3dacbd8515d38 to your computer and use it in GitHub Desktop.
Save Mu-adventofcode/373d42ff22236f9dcbc3dacbd8515d38 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 12 part 2
"""
Advent of Code 2021 day 12 part 2.
"""
from collections import defaultdict
def count_paths(curpath):
path_count = 0
cave = curpath[-1]
twice_done = (
# wether we have crossed any small room twice:
max(curpath.count(c) for c in curpath if c.islower())
> 1
)
for adj in adjacents[cave]:
if adj == "start":
continue
if adj.islower() and adj in curpath and twice_done:
continue
if adj == "end":
path_count += 1
else:
path_count += count_paths(curpath + [adj])
return path_count
adjacents = defaultdict(set)
with open("input_12.txt") as f:
for line in f:
c1, c2 = tuple(line.strip().split("-"))
adjacents[c1].add(c2)
adjacents[c2].add(c1)
print(count_paths(["start"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment