-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.py
40 lines (28 loc) · 832 Bytes
/
12.py
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
36
37
38
39
40
from utils import iter_lines, rinput
from collections import defaultdict
def parse(inp):
graph = defaultdict(set)
for line in iter_lines(inp):
frm, to = line.split("-")
graph[frm].add(to)
graph[to].add(frm)
return graph
def solve1(inp):
graph = parse(inp)
return dfs(graph, "start", set(), True)
def dfs(G, v, seen, revisiting):
if v == "end":
return 1
paths_cnt = 0
for w in G[v]:
if w == "start":
continue
elif w not in seen or w.isupper():
paths_cnt += dfs(G, w, seen | {v}, revisiting)
elif not revisiting:
paths_cnt += dfs(G, w, seen | {v}, True)
return paths_cnt
def solve2(inp):
return dfs(parse(inp), "start", set(), False)
print(solve1(rinput(12)))
print(solve2(rinput(12)))