-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.py
78 lines (74 loc) · 2.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
with open('data/test/12.test') as f:
testlines = [ line.strip() for line in f]
with open('data/my_input/12.in') as f:
lines = [ line.strip() for line in f]
def part1(v):
d=dict()
se=set()
lowerse=set()
for i,j in enumerate(v):
a,b= j.split("-")
d[(a,b)]=1
se.add(a)
se.add(b)
if a.islower():
lowerse.add(a)
if b.islower():
lowerse.add(b)
currentnode=['start,']
allpossibleways=set()
while currentnode:
cncs=currentnode.pop()
cn=cncs.split(",")[-2]
possiblematches=[k[0] for k in d.keys() if k[1]==cn]
possiblematches.extend([k[1] for k in d.keys() if k[0]==cn])
for i,j in enumerate(possiblematches):
if j=='end':
allpossibleways.add(cncs+j)
elif j.islower() and j not in cncs:
currentnode.append(cncs+j+",")
elif j.isupper():
currentnode.append(cncs+j+",")
return len(allpossibleways)
def usedjoker(s,se):
for i,j in enumerate(se):
if s.count(j+',') >=2:
return True
return False
def part2(v):
d=dict()
se=set()
lowerse=set()
count=0
for i,j in enumerate(v):
a,b= j.split("-")
d[(a,b)]=1
se.add(a)
se.add(b)
if a.islower():
lowerse.add(a)
if b.islower():
lowerse.add(b)
currentnode=['start,']
allpossibleways=set()
visited_lower=set()
while currentnode:
cncs=currentnode.pop()
cn=cncs.split(",")[-2]
possiblematches=[k[0] for k in d.keys() if k[1]==cn]
possiblematches.extend([k[1] for k in d.keys() if k[0]==cn])
for i,j in enumerate(possiblematches):
if j=='end':
allpossibleways.add(cncs+j)
elif j.islower() and j !='start' :
if not usedjoker(cncs,lowerse) :
currentnode.append(cncs+j+",")
elif cncs.count(j)<1:
currentnode.append(cncs+j+",")
elif j.isupper():
currentnode.append(cncs+j+",")
return len(allpossibleways)
print("part1 test output",part1(testlines))
print("part1 my output",part1(lines))
print("part2 test output",part2(testlines))
print("part2 my output",part2(lines))