summary refs log blame commit diff stats
path: root/day12.py
blob: 55732b51027aea4a95aee93c7160917a2ff86eff (plain) (tree)


































                                                                    
#!/usr/bin/env python

from collections import defaultdict

graph = defaultdict(list)
with open("day12.txt") as data:
    for line in data:
        vert1, vert2 = line.strip().split('-')
        if vert2 != "start":
            graph[vert1].append(vert2)
        if vert1 != "start":
            graph[vert2].append(vert1)
    del graph["end"]

# part 1
def walk(path=['start']):
    count = 0
    for point in graph[path[-1]]:
        if point.isupper() or not point in path:
            count += 1 if point == 'end' else walk(path + [point])
    return count

print(walk())

# part 2
def walk2(path=['start']):
    count = 0
    for point in graph[path[-1]]:
        count += 1 if point == 'end' else (walk2,
                                           walk)[point.islower()
                                                 and point in path](
                                                     path + [point])
    return count

print(walk2())