-
Notifications
You must be signed in to change notification settings - Fork 1
/
12.py
63 lines (38 loc) · 1.09 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import fileinput
from collections import defaultdict
G = defaultdict(set)
for l in fileinput.input():
x, y = l.strip().split('-')
G[x].add(y)
G[y].add(x)
def dfs(u, visited, current_path, paths, visiting_condition):
if u.islower():
visited[u] += 1
current_path.append(u)
if u == 'end':
paths.add(str(current_path))
visited[u] -= 1
current_path.pop()
return
for adj in G[u]:
if visiting_condition(adj, visited):
dfs(adj, visited, current_path, paths, visiting_condition)
current_path.pop()
if u.islower():
visited[u] -= 1
# Part 1
paths = set()
dfs('start', defaultdict(int), [], paths, lambda cave, visited: cave.isupper() or visited[cave] < 1)
print(len(paths))
# Part 2
def can_go(cave, visited):
if cave == 'start':
return False
if cave.isupper():
return True
if len(visited) == 0 or max(visited.values()) > 1:
return visited[cave] < 1
return visited[cave] < 2
paths = set()
dfs('start', defaultdict(int), [], paths, can_go)
print(len(paths))