Created
May 6, 2022 18:17
-
-
Save Mu-adventofcode/373d42ff22236f9dcbc3dacbd8515d38 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 12 part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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