summary refs log tree commit diff stats
path: root/day12.py
blob: 55732b51027aea4a95aee93c7160917a2ff86eff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/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())