-
Notifications
You must be signed in to change notification settings - Fork 1
/
day10_adapters.py
131 lines (103 loc) · 2.91 KB
/
day10_adapters.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
import functools
from typing import List, Set
import pytest
def device_joltage_adapter(adapters: List[int]):
return max(adapters) + 3
@pytest.fixture
def few_adapters():
TEST_ADAPTERS = """16
10
15
5
1
11
7
19
6
12
4"""
return [int(rating) for rating in TEST_ADAPTERS.split("\n")]
# @pytest.fixture
def more_adapters():
TEST_ADAPTERS = """28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3"""
return [int(rating) for rating in TEST_ADAPTERS.split("\n")]
def test_device_joltage_adapter(few_adapters):
assert device_joltage_adapter(few_adapters) == 22
def use_all_adapters(adapters: List[int]):
adapters_left = set(adapters)
adapters_left.add(device_joltage_adapter(adapters_left))
adapters_left.add(0) # charging outlet
difference_of_1 = 0
difference_of_3 = 0
while len(adapters_left) > 1:
current_adapter = min(adapters_left)
adapters_left.remove(current_adapter)
next_adapter = min(adapters_left)
difference = next_adapter - current_adapter
if difference == 1:
difference_of_1 += 1
elif difference == 3:
difference_of_3 += 1
else:
raise ValueError
return (difference_of_1, difference_of_3)
def test_use_all_adapters_1(few_adapters):
difference_of_1, difference_of_3 = use_all_adapters(few_adapters)
assert difference_of_1 * difference_of_3 == 35
def test_use_all_adapters_2(more_adapters):
difference_of_1, difference_of_3 = use_all_adapters(more_adapters)
assert difference_of_1 * difference_of_3 == 22 * 10
@functools.lru_cache()
def count_adapter_paths(adapters_str: str, current_adapter: int):
adapters = [int(value) for value in adapters_str.split(",")]
max_adapters = max(adapters)
if current_adapter == max_adapters:
return 1
paths = 0
if current_adapter + 1 in adapters:
paths += count_adapter_paths(adapters_str, current_adapter + 1)
if current_adapter + 2 in adapters:
paths += count_adapter_paths(adapters_str, current_adapter + 2)
if current_adapter + 3 in adapters:
paths += count_adapter_paths(adapters_str, current_adapter + 3)
return paths
def test_count_adapter_paths(few_adapters, more_adapters):
assert count_adapter_paths(few_adapters, current_adapter=0) == 8
assert count_adapter_paths(more_adapters, current_adapter=0) == 19208
if __name__ == "__main__":
with open("2020/data/day10_input.txt") as f:
adapters = [int(rating) for rating in f.readlines()]
difference_of_1, difference_of_3 = use_all_adapters(adapters)
print(difference_of_1 * difference_of_3)
adapters_str = ",".join(str(value) for value in adapters)
print(count_adapter_paths(adapters_str, current_adapter=0))