-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (58 loc) · 2.14 KB
/
main.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
from collections import defaultdict
class Day12:
def __init__(self):
self.puzzle_input = self.get_puzzle_input()
self.graph = self.get_connected_caves()
self.paths = []
def part_one(self) -> int:
graph = self.graph.copy()
self.get_cave_paths(graph, 'start', [], single=True)
return len(self.paths)
def part_two(self) -> int:
graph = self.graph.copy()
self.get_cave_paths(graph, 'start', [], single=False)
return len(self.paths)
def get_connected_caves(self) -> defaultdict:
data = self.puzzle_input.copy()
graph = defaultdict(list)
for line in data:
path_from, path_to = line.split('-')
graph[path_from].append(path_to)
if path_from != 'start':
graph[path_to].append(path_from)
return graph
def get_cave_paths(self, graph, current_cave, path, single) -> None:
path.append(current_cave)
if current_cave == 'end':
if path not in self.paths:
self.paths.append(path)
return
for i in graph[current_cave]:
path_copy = path.copy()
if i == 'start' and 'start' in path_copy:
continue
# Already visited
if i.islower() and i in path_copy:
# P1 or P2
if single or self.visited_twice(path_copy):
continue
# No return back
if len(graph[current_cave]) == 1 and path_copy[-2].islower() and graph[current_cave] == path_copy[-2]:
continue
self.get_cave_paths(graph, i, path_copy, single)
@staticmethod
def visited_twice(path) -> bool:
for cave in path:
if cave.islower() and path.count(cave) == 2:
return True
return False
@staticmethod
def get_puzzle_input() -> list:
lst = []
with open("input.txt") as file:
for line in file:
lst.append(line.rstrip())
return lst
day = Day12()
print(f'Result part 1: {day.part_one()}')
print(f'Result part 2: {day.part_two()}')