2020 Day 10

This commit is contained in:
Luke Hubmayer-Werner 2020-12-10 16:26:27 +10:30
parent 0bf886b0fe
commit d368a2cac3
3 changed files with 125 additions and 0 deletions

0
2019/day16.py Normal file
View File

22
2020/day10.py Normal file
View File

@ -0,0 +1,22 @@
import numpy as np
# Load input
with open('input10', 'r') as f:
data = [0] + sorted([int(l) for l in f.readlines()]) # NB: We add the 0J source ourselves
n_data = np.array(data)
# Part 1: Connect all of them together
diff = n_data[1:] - n_data[:-1]
# Confirm this by multiplying the 1J differences with the 3J differences.
# NB: we treat the internal adapter 3J difference as an off-by-one for simplicity.
print('Part 1: ', (diff==1).sum() * (1 + (diff==3).sum()))
# Part 2: Find the number of possible chains
n_possibilities = {data[-1]: 1} # The highest one can only connect to the internal adapter.
for i in reversed(data[:-1]): # We've already done the highest one as a special case
delta = n_data - i
p = 0
for n in n_data[np.logical_and(delta<=3, delta>0)]: # You could probably plug two of the same together, but our input doesn't require us to handle that, and this simplifies things ;)
p += n_possibilities[n]
n_possibilities[i] = p
print('Part 2: ', n_possibilities[0])

103
2020/input10 Normal file
View File

@ -0,0 +1,103 @@
73
114
100
122
10
141
89
70
134
2
116
30
123
81
104
42
142
26
15
92
56
60
3
151
11
129
167
76
18
78
32
110
8
119
164
143
87
4
9
107
130
19
52
84
55
69
71
83
165
72
156
41
40
1
61
158
27
31
155
25
93
166
59
108
98
149
124
65
77
88
46
14
64
39
140
95
113
54
66
137
101
22
82
21
131
109
45
150
94
36
20
33
49
146
157
99
7
53
161
115
127
152
128