-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 12.py
69 lines (61 loc) · 1.99 KB
/
Day 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
64
65
66
67
68
69
test = '''start-A
start-b
A-c
A-b
b-d
A-end
b-end'''.split('\n')
inp = open('Day 12.txt','r').read().split('\n')
import copy
def part1(inp):
connections = {}
for i in inp:
if i.split('-')[0] not in connections.keys():
connections[i.split('-')[0]] = []
if i.split('-')[1] != 'start':
connections[i.split('-')[0]] += [i.split('-')[1]]
if i.split('-')[1] not in connections.keys():
connections[i.split('-')[1]] = []
if i.split('-')[0] != 'start':
connections[i.split('-')[1]] += [i.split('-')[0]]
done = 0
paths = [['start']]
while len(paths) > 0:
path = paths.pop(-1)
for i in connections[path[-1]]:
if i.islower() and i in path:
continue
paths.append(path + [i])
if paths[-1][-1] == 'end':
done += 1
paths.remove(path + [i])
print(done)
def visited(inp):
if [inp.count(i) for i in inp if i.islower()].count(2) == 2:
return True
return False
def part2(inp):
connections = {}
for i in inp:
if i.split('-')[0] not in connections.keys():
connections[i.split('-')[0]] = []
if i.split('-')[1] != 'start':
connections[i.split('-')[0]] += [i.split('-')[1]]
if i.split('-')[1] not in connections.keys():
connections[i.split('-')[1]] = []
if i.split('-')[0] != 'start':
connections[i.split('-')[1]] += [i.split('-')[0]]
done = 0
paths = [['start']]
while len(paths) > 0:
path = paths.pop(-1)
for i in connections[path[-1]]:
if i.islower() and (path.count(i) == 2 or (i in path and visited(path))):
continue
paths.append(path + [i])
if paths[-1][-1] == 'end':
done += 1
paths.remove(path + [i])
print(done)
part1(inp)
part2(inp)