-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12b.py
More file actions
executable file
·48 lines (40 loc) · 1.11 KB
/
12b.py
File metadata and controls
executable file
·48 lines (40 loc) · 1.11 KB
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
#!/usr/bin/env python3
import numpy as np
import re
with open('12.input', 'r') as f:
segs = re.findall('(\w+)-(\w+)', f.read())
m = {}
for seg in segs:
a, b = seg
if a in m:
m[a].append(b)
else:
m[a] = [b]
if b in m:
m[b].append(a)
else:
m[b] = [a]
sol = []
mem = {}
for k in m:
mem[k] = 0
def has_visit_single_twice(mem):
for memkey in mem:
if memkey.islower() and mem[memkey] >= 2:
return True
return False
def move(curr, trail, m, mem):
for dest in m[curr]:
if dest == 'start':
continue
if not dest.isupper() and mem[dest] > 0 and has_visit_single_twice(mem):
continue
else:
if dest == 'end':
sol.append(trail + [curr, 'end'])
else:
memcop = dict(mem)
memcop[dest] += 1
move(dest, trail + [curr], m, memcop)
move('start', [], m, mem)
print(len(sol))