Skip to content

Commit 567d21f

Browse files
committed
Day 12
1 parent 17309ad commit 567d21f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

12/1.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
from typing import List
3+
4+
nodes = {}
5+
6+
7+
def find_path(current_node: str, destination: str, visited: List, paths: List):
8+
if current_node != destination and current_node.islower() and current_node in visited:
9+
return
10+
11+
visited = list(visited)
12+
visited.append(current_node)
13+
14+
if current_node == destination:
15+
paths.append(visited)
16+
return
17+
18+
for next_node in nodes[current_node]:
19+
find_path(next_node, destination, visited, paths)
20+
21+
22+
def solve():
23+
paths = []
24+
find_path('start', 'end', [], paths)
25+
return len(paths)
26+
27+
28+
if __name__ == "__main__":
29+
with open('./input', 'r') as f:
30+
lines = f.read().splitlines()
31+
for line in lines:
32+
parent, child = line.split('-')
33+
if parent not in nodes:
34+
nodes[parent] = []
35+
if child not in nodes:
36+
nodes[child] = []
37+
if child not in nodes[parent]:
38+
nodes[parent].append(child)
39+
if parent not in nodes[child]:
40+
nodes[child].append(parent)
41+
print(solve())

12/2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
from typing import List
3+
4+
nodes = {}
5+
6+
7+
def find_path(current_node: str, destination: str, visited: List, paths: List):
8+
if current_node == 'start' and len(visited) > 1:
9+
return
10+
11+
if current_node.islower() and current_node in visited and max([visited.count(n) for n in visited if n.islower()]) > 1:
12+
return
13+
14+
visited = list(visited)
15+
visited.append(current_node)
16+
17+
if current_node == destination:
18+
paths.append(visited)
19+
return
20+
21+
for next_node in nodes[current_node]:
22+
find_path(next_node, destination, visited, paths)
23+
24+
25+
def solve():
26+
paths = []
27+
find_path('start', 'end', [], paths)
28+
return len(paths)
29+
30+
31+
if __name__ == "__main__":
32+
with open('./input', 'r') as f:
33+
lines = f.read().splitlines()
34+
for line in lines:
35+
parent, child = line.split('-')
36+
if parent not in nodes:
37+
nodes[parent] = []
38+
if child not in nodes:
39+
nodes[child] = []
40+
if child not in nodes[parent]:
41+
nodes[parent].append(child)
42+
if parent not in nodes[child]:
43+
nodes[child].append(parent)
44+
print(solve())

12/input

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pn-TY
2+
rp-ka
3+
az-aw
4+
al-IV
5+
pn-co
6+
end-rp
7+
aw-TY
8+
rp-pn
9+
al-rp
10+
end-al
11+
IV-co
12+
end-TM
13+
co-TY
14+
TY-ka
15+
aw-pn
16+
aw-IV
17+
pn-IV
18+
IV-ka
19+
TM-rp
20+
aw-PD
21+
start-IV
22+
start-co
23+
start-pn

0 commit comments

Comments
 (0)