-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (48 loc) · 2.05 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
from collections import defaultdict, Counter
class Day10:
def __init__(self):
self.puzzle_input = self.load_input()
self.memoize = {}
def part_one(self) -> int:
charging_outlet, built_in_adapter = 0, max(self.puzzle_input) + 3
self.puzzle_input.extend([charging_outlet, built_in_adapter])
self.puzzle_input.sort()
# Differences between each element
differences = [self.puzzle_input[i] - self.puzzle_input[i - 1] for i in range(1, len(self.puzzle_input))]
difference_counts = Counter(differences)
number = 1
for occurrences in difference_counts.values():
number *= occurrences
return number
def part_two(self) -> int:
self.puzzle_input.reverse()
connected_adapters = self.get_connected_adapters(self.puzzle_input)
return self.get_distinct_ways(connected_adapters, max(self.puzzle_input), min(self.puzzle_input))
@staticmethod
def get_connected_adapters(puzzle_input: list) -> defaultdict(list):
adapters = defaultdict(list)
for i in range(len(puzzle_input)):
for j in range(i + 1, len(puzzle_input)):
if (puzzle_input[i] - 3) <= puzzle_input[j] <= puzzle_input[i]:
adapters[puzzle_input[i]].append(puzzle_input[j])
return adapters
def get_distinct_ways(self, adapters: defaultdict(list), adapter: int, min: int) -> int:
if adapter == min:
return 1
if adapter in self.memoize:
return self.memoize[adapter]
count = 0
for next_adapter in adapters[adapter]:
count += self.get_distinct_ways(adapters, next_adapter, min)
self.memoize[adapter] = count
return count
@staticmethod
def load_input() -> list:
list_input = []
with open("input.txt") as file:
for line in file:
list_input.append(int(line.rstrip()))
return list_input
day = Day10()
print(f"Result part 1: {day.part_one()}")
print(f"Result part 2: {day.part_two()}")