Solve day 10 part 1

This commit is contained in:
Gabriel Augendre 2020-12-10 08:36:39 +01:00
parent 039182bea9
commit 640c7f3cc8
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
4 changed files with 188 additions and 0 deletions

View file

@ -0,0 +1,42 @@
import copy
import itertools
from typing import List
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
print(f"\n+ Running on {filename}")
with open(filename) as f:
jolts = sorted(map(int, f.read().strip().split("\n"))) # type: List[int]
jolts.append(jolts[-1] + 3)
counter_part_1 = solve_part_1(jolts)
counter_part_2 = 0
print(f"1. Found {counter_part_1}")
if expected_part_1:
assert expected_part_1 == counter_part_1
print(f"2. Found {counter_part_2}")
if expected_part_2:
assert expected_part_2 == counter_part_2
def solve_part_1(jolts):
diffs = count_diffs(jolts)
return diffs[1] * diffs[3]
def count_diffs(jolts):
diffs = {1: 0, 2: 0, 3: 0}
previous = 0
for jolt in jolts:
diffs[jolt - previous] += 1
previous = jolt
return diffs
if __name__ == "__main__":
main("inputs/day10-test1", 35, 8)
main("inputs/day10-test2", 220, 19208)
main("inputs/day10", 2450)

104
2020/inputs/day10 Normal file
View file

@ -0,0 +1,104 @@
2
49
78
116
143
42
142
87
132
86
67
44
136
82
125
1
108
123
46
37
137
148
106
121
10
64
165
17
102
156
22
117
31
38
24
69
131
144
162
63
171
153
90
9
107
79
7
55
138
34
52
77
152
3
158
100
45
129
130
135
23
93
96
103
124
95
8
62
39
118
164
172
75
122
20
145
14
112
61
43
141
30
85
101
151
29
113
94
68
58
76
97
28
111
128
21
11
163
161
4
168
157
27
72

11
2020/inputs/day10-test1 Normal file
View file

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4

31
2020/inputs/day10-test2 Normal file
View file

@ -0,0 +1,31 @@
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