-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12_task2.py
More file actions
68 lines (54 loc) · 1.34 KB
/
Copy pathday12_task2.py
File metadata and controls
68 lines (54 loc) · 1.34 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
###############################################################################
# Day 12, Task 1 #
###############################################################################
import aoc_util
day = 12
#data_str = """fs-end
"""he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW"""
data_str = """start-A
start-b
A-c
A-b
b-d
A-end
b-end"""
def options_from(paths, visited, double, curr_node):
option_count = 0
visited = visited.copy()
if curr_node.islower():
visited.append(curr_node)
if curr_node == "end":
option_count = 1
else:
next_nodes = paths[curr_node]
for i in next_nodes:
if i not in visited:
option_count += options_from(paths, visited, double, i)
elif not double and i != "start" and i != "end":
option_count += options_from(paths, visited, i, i)
return option_count
def task(data_set: list[str]) -> int:
paths = {}
for i in data_set:
[start, end] = i.split("-")
paths.setdefault(start, []).append(end)
paths.setdefault(end, []).append(start)
return options_from(paths, [], None, "start")
aoc_util.run_with_data_str(task, data_str)
aoc_util.run_with_data_set(task, day)