-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12.py
More file actions
63 lines (43 loc) · 1.2 KB
/
day12.py
File metadata and controls
63 lines (43 loc) · 1.2 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
# Advent of Code 2021, Day 12
# markusremplbauer
import networkx as nx
from aocd.models import Puzzle
from funcy import ilen, print_calls
START, END = "start", "end"
@print_calls
def part1(graph):
return ilen(dfs(graph, [START]))
@print_calls
def part2(graph):
return ilen(dfs(graph, [START], double_visit=True))
def dfs(graph, path, double_visit=False):
for n in graph.neighbors(path[-1]):
if n == START:
continue
if (
n.islower()
and n in path
and (
not double_visit or any(path.count(e) > 1 and e.islower() for e in path)
)
):
continue
new_path = path + [n]
if n == END:
yield new_path
continue
yield from dfs(graph, new_path, double_visit=double_visit)
def load(data):
graph = nx.Graph()
for line in data.splitlines():
a, b = line.split("-")
graph.add_edge(a, b)
return graph
def main():
puzzle = Puzzle(year=2021, day=12)
ans1 = part1(load(puzzle.input_data))
# puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
# puzzle.answer_b = ans2
if __name__ == "__main__":
main()