blob: a5ed7471909ebbd3554a12a68c2811dd969366bf (
plain)
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
|
import sys
from collections import defaultdict, deque
data = sys.argv[1] if len(sys.argv) > 1 else "day12data.txt"
E = defaultdict(list)
for line in open(data):
a, b = line.strip().split("-")
E[a].append(b)
E[b].append(a)
def solve(p1):
start = ("start", set(["start"]), None)
ans = 0
Q = deque([start])
while Q:
pos, small, twice = Q.popleft()
if pos == "end":
ans += 1
continue
for y in E[pos]:
if y not in small:
newSmall = set(small)
if y.lower() == y:
newSmall.add(y)
Q.append((y, newSmall, twice))
elif y in small and twice is None and y not in ["start", "end"] and not p1:
Q.append((y, small, y))
return ans
print(solve(p1=True))
print(solve(p1=False))
|