-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_12.py
139 lines (116 loc) · 3.09 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""AOC Day 12"""
import pathlib
import time
from collections import defaultdict, deque
TEST_INPUT_1 = """start-A
start-b
A-c
A-b
b-d
A-end
b-end"""
TEST_INPUT_2 = """dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc"""
TEST_INPUT_3 = """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"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> defaultdict:
"""take input data and return the appropriate data structure"""
graph = defaultdict(list)
entries = input_data.split('\n')
for entry in entries:
a, b = entry.split('-')
if b != 'start':
graph[a].append(b)
if a != 'start':
graph[b].append(a)
return graph
def count_paths_1(graph: defaultdict, src: str, dst: str) -> int:
stack = deque([(src, {src})])
total = 0
while stack:
node, visited = stack.pop()
if node == dst:
total += 1
continue
for n in graph[node]:
if n in visited and n.islower():
continue
stack.append((n, visited | {n}))
return total
def count_paths_2(graph: defaultdict, src: str, dst: str) -> int:
stack = deque([(src, {src}, False)])
total = 0
while stack:
node, visited, double = stack.pop()
if node == dst:
total += 1
continue
for n in graph[node]:
if n not in visited or n.isupper():
stack.append((n, visited | {n}, double))
continue
if double:
continue
stack.append((n, visited, True))
return total
def part1(graph: defaultdict) -> int:
"""part1 solver take the entries and return the part1 solution"""
return count_paths_1(graph, 'start', 'end')
def part2(graph: defaultdict) -> int:
"""part2 solver take the entries and return the part2 solution"""
return count_paths_2(graph, 'start', 'end')
def test_input_day_12():
"""pytest testing function"""
entries_1 = extract(TEST_INPUT_1)
entries_2 = extract(TEST_INPUT_2)
entries_3 = extract(TEST_INPUT_3)
assert part1(entries_1) == 10
assert part1(entries_2) == 19
assert part1(entries_3) == 226
assert part2(entries_1) == 36
assert part2(entries_2) == 103
assert part2(entries_3) == 3509
def test_bench_day_12(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
print("Part 1: %d" % part1(entries))
print("Part 2: %d" % part2(entries))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()