-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 9.dib
303 lines (212 loc) · 9.26 KB
/
day 9.dib
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!meta
{"kernelInfo":{"defaultKernelName":"fsharp","items":[{"aliases":[],"languageName":"fsharp","name":"fsharp"}]}}
#!markdown
# --- Day 9: Mirage Maintenance ---
You ride the camel through the sandstorm and stop where the ghost's maps told you to stop. The sandstorm subsequently subsides, somehow seeing you standing at an oasis!
The camel goes to get some water and you stretch your neck. As you look up, you discover what must be yet another giant floating island, this one made of metal! That must be where the parts to fix the sand machines come from.
There's even a hang glider partially buried in the sand here; once the sun rises and heats up the sand, you might be able to use the glider and the hot air to get all the way up to the metal island!
While you wait for the sun to rise, you admire the oasis hidden here in the middle of Desert Island. It must have a delicate ecosystem; you might as well take some ecological readings while you wait. Maybe you can report any environmental instabilities you find to someone so the oasis can be around for the next sandstorm-worn traveler.
You pull out your handy Oasis And Sand Instability Sensor and analyze your surroundings. The OASIS produces a report of many values and how they are changing over time (your puzzle input). Each line in the report contains the history of a single value. For example:
```
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
```
To best protect the oasis, your environmental report should include a prediction of the next value in each history. To do this, start by making a new sequence from the difference at each step of your history. If that sequence is not all zeroes, repeat this process, using the sequence you just generated as the input sequence. Once all of the values in your latest sequence are zeroes, you can extrapolate what the next value of the original history should be.
In the above dataset, the first history is 0 3 6 9 12 15. Because the values increase by 3 each step, the first sequence of differences that you generate will be 3 3 3 3 3. Note that this sequence has one fewer value than the input sequence because at each step it considers two numbers from the input. Since these values aren't all zero, repeat the process: the values differ by 0 at each step, so the next sequence is 0 0 0 0. This means you have enough information to extrapolate the history! Visually, these sequences can be arranged like this:
```
0 3 6 9 12 15
3 3 3 3 3
0 0 0 0
```
To extrapolate, start by adding a new zero to the end of your list of zeroes; because the zeroes represent differences between the two values above them, this also means there is now a placeholder in every sequence above it:
```
0 3 6 9 12 15 B
3 3 3 3 3 A
0 0 0 0 0
```
You can then start filling in placeholders from the bottom up. A needs to be the result of increasing 3 (the value to its left) by 0 (the value below it); this means A must be 3:
```
0 3 6 9 12 15 B
3 3 3 3 3 3
0 0 0 0 0
```
Finally, you can fill in B, which needs to be the result of increasing 15 (the value to its left) by 3 (the value below it), or 18:
```
0 3 6 9 12 15 18
3 3 3 3 3 3
0 0 0 0 0
```
So, the next value of the first history is 18.
Finding all-zero differences for the second history requires an additional sequence:
```
1 3 6 10 15 21
2 3 4 5 6
1 1 1 1
0 0 0
```
Then, following the same process as before, work out the next value in each sequence from the bottom up:
```
1 3 6 10 15 21 28
2 3 4 5 6 7
1 1 1 1 1
0 0 0 0
```
So, the next value of the second history is 28.
The third history requires even more sequences, but its next value can be found the same way:
```
10 13 16 21 30 45 68
3 3 5 9 15 23
0 2 4 6 8
2 2 2 2
0 0 0
```
So, the next value of the third history is 68.
If you find the next value for each history in this example and add them together, you get 114.
Analyze your OASIS report and extrapolate the next value for each history. What is the sum of these extrapolated values?
#!fsharp
type PolynomialSequence = int list
let rec parsePolynomialSequence (result: int list) (nums: int array) =
if nums |> Array.forall ((=) 0)
then result |> List.rev
else
let next =
Seq.pairwise nums
|> Seq.map (fun (x, y) -> y - x)
|> Array.ofSeq
parsePolynomialSequence (nums[0]::result) next
let parseLine (input: string) =
input.Split(' ', StringSplitOptions.RemoveEmptyEntries)
|> Array.map int
|> parsePolynomialSequence []
let sampleInput =
[|
"0 3 6 9 12 15"
"1 3 6 10 15 21"
"10 13 16 21 30 45"
|]
|> Array.map parseLine
sampleInput
#!fsharp
let rec fnDoesntWork (c: PolynomialSequence) (i: int) =
match c, i with
| _, -1 -> 0
| [], _ -> 0
| cn::tail, _ -> i * (fnDoesntWork tail (i-1)) + cn
sampleInput
|> Array.map (fun input -> Seq.map (fnDoesntWork input) { 0..5 })
#!markdown
Need to think through this more appropriately.
When there is one layer, it is a linear equation
`y = mx + c`
When there are two layers, it is a quadratic equation
The bottom layer is linear, still
`y = mx + c`. However, there is one fewer number in the list.
The second layer from the bottom is quadratic. In the sample
```
1 3 6 10 15 21
2 3 4 5 6
1 1 1 1
```
The top layer is
```
= sum i=0..4 (2 + i) + 1
= (2 + 0) + (2 + 1) + (2 + 2) + (2 + 3) + (2 + 4) + 1
```
Let's go with the summation approach for now, or even a sequence generator. I'm sure there's a mathematical function that would generate this, but not sure how to construct it at this stage.
#!fsharp
let rec fnSeq' (op: int->int->int) (input: PolynomialSequence) (state: int array) =
seq {
yield state[0]
let intermediate =
if state.Length = input.Length
then state
else Array.append state [| op input[state.Length] 0 |]
let folder i state = (op i state, op i state)
let (next, _) =
(intermediate, 0)
||> Array.mapFoldBack folder
yield! fnSeq' op input next
}
let forwardFnSeq (input: PolynomialSequence) = fnSeq' (+) input [| input[0] |]
sampleInput
|> Array.map (forwardFnSeq >> Seq.take 7)
#!fsharp
sampleInput
|> Seq.map (forwardFnSeq >> Seq.item 6)
|> Seq.sum
#!markdown
That works. Let's see if it will work for the challenge input
#!fsharp
let challengeInputLines =
File.ReadAllLinesAsync "day 9 input.txt"
|> Async.AwaitTask
|> Async.RunSynchronously
let challengeInput =
challengeInputLines
|> Array.map parseLine
challengeInputLines
|> Array.map (fun x -> x.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|> Array.groupBy (fun x -> x.Length)
#!markdown
Cool, every input sequence is 21 entries long. We should be able to solve the challenge by grabbing the 22nd (index=21) entry for each sequence and summing them.
#!fsharp
challengeInput
|> Seq.map (forwardFnSeq >> Seq.item 21)
|> Seq.sum
#!markdown
## --- Part Two ---
Of course, it would be nice to have even more history included in your report. Surely it's safe to just extrapolate backwards as well, right?
For each history, repeat the process of finding differences until the sequence of differences is entirely zero. Then, rather than adding a zero to the end and filling in the next values of each previous sequence, you should instead add a zero to the beginning of your sequence of zeroes, then fill in new first values for each previous sequence.
In particular, here is what the third example history looks like when extrapolating back in time:
```
5 10 13 16 21 30 45
5 3 3 5 9 15
-2 0 2 4 6
2 2 2 2
0 0 0
```
Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: 5.
Doing this for the remaining example data above results in previous values of -3 for the first history and 0 for the second history. Adding all three new values together produces 2.
Analyze your OASIS report again, this time extrapolating the previous value for each history. What is the sum of these extrapolated values?#
#!fsharp
let revFnSeq (input: PolynomialSequence) = fnSeq' (-) input (Array.ofList input)
sampleInput[2]
|> revFnSeq
|> Seq.take 2
#!fsharp
sampleInput
|> Array.map (revFnSeq >> Seq.item 1)
|> Array.sum
#!fsharp
challengeInput
|> Array.map (revFnSeq >> Seq.item 1)
|> Array.sum
#!markdown
I want to have a go at making the function cleaner. Let's try where a sequence depends on other sequences
#!fsharp
let rec fnSeq'' (op: int->int->int) (skip: int) (input: PolynomialSequence) =
match input with
| [] -> seq {
while true do
yield 0
}
| head::tail ->
fnSeq'' op skip tail
|> Seq.scan (fun i next -> op i next) head
|> Seq.skip skip
let forwardFnSeq' = fnSeq'' (+) 0
let revFnSeq' = fnSeq'' (-) 1
sampleInput
|> Array.map (forwardFnSeq' >> Seq.take 7)
#!fsharp
challengeInput
|> Seq.map (forwardFnSeq >> Seq.item 21)
|> Seq.sum
#!fsharp
sampleInput
|> Array.map (revFnSeq' >> Seq.take 3)
#!fsharp
challengeInput
|> Array.map (revFnSeq' >> Seq.head)
|> Array.sum