-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
51 lines (35 loc) · 1.07 KB
/
day12.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
from collections import defaultdict
from copy import copy
data = open("input.txt").read().strip().splitlines()
graph = defaultdict(list)
for line in data:
a, b = map(str.strip, line.split('-'))
graph[a].append(b)
graph[b].append(a)
# one star
def dfs(node, visited):
if node == 'end': return 1
if node.islower(): visited.add(node)
c = 0
for n in graph[node]:
if n in visited: continue
c += dfs(n, copy(visited))
return c
print(dfs('start', {'start'}))
# two stars
def dfs2(node, visited):
if node == 'end': return 1
if node.islower():
visited[node] += 1
c = 0
small_visited_twice = any([x == 2 for x in visited.values()])
for n in graph[node]:
if visited[n] == 2: continue
if visited[n] == 1 and small_visited_twice: continue
c += dfs2(n, copy(visited))
return c
for k, v in graph.items():
if k != 'start' and 'start' in v:
v.remove('start')
graph['end'] = []
print(dfs2('start', defaultdict(int, {'start': 0})))