-
Notifications
You must be signed in to change notification settings - Fork 1
/
day06.html
468 lines (438 loc) Β· 30 KB
/
day06.html
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2021/day06.nim</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>π³</text></svg>">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/pietroppeter/nimib/assets/androidstudio.css'>
<style>
.nb-box {
display: flex;
align-items: center;
justify-content: space-between;
}
.nb-small {
font-size: 0.8rem;
}
button.nb-small {
float: right;
padding: 2px;
padding-right: 5px;
padding-left: 5px;
}
section#source {
display:none
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous" onload="renderMathInElement(document.body,{delimiters:[{left: '$$', right: '$$', display: true},{left: '$', right: '$', display: false}]});"></script>
<script async defer data-domain="pietroppeter.github.io/adventofnim" src="https://plausible.io/js/plausible.js"></script>
<style>
a {
text-decoration: none;
color: #009900;
}
a:hover {
color: #99ff99;
}
em {
color: #ffffff;
font-style: normal;
text-shadow: 0 0 5px #ffffff;
}
em.star {
font-style: normal;
color: #ffff66;
text-shadow: 0 0 5px #ffff66;
}
</style>
</head>
<body>
<header>
<div class="nb-box">
<span><a href="..">π‘</a></span>
<span><code>2021/day06.nim</code></span>
<span><a href="https://github.com/pietroppeter/adventofnim"><svg aria-hidden="true" width="1.2em" height="1.2em" style="vertical-align: middle; fill: #fff" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path></svg></a></span>
</div>
<hr>
</header><main>
<h2>Day 6: <a href="https://adventofcode.com/2021/day/6">Lanternfish</a> π£π</h2>
<p>Today is the classical case of: "simple (brute force) approach
will work for part1 but it will not work for part2. Also it
is the kind of the day where I tend to try and derive mathematical
properties on some functions and get lost along the way
(like <a href="../2020/day10hints.html">last year</a>).</p>
<p>This year these days I have some catch up to do
(still have to post day 4 and 5, lots of viz missing)
so I will go with the first stuff that comes to mind,
the end result is not great so you might want to skip
to the end where I give links to better solutions by advent-of-nim-mers.</p>
<h3>Part 1</h3>
<p>If you are still here, well,
I grew fond of the include trick that I used in <a href="day02.html">day02</a>
that I am using it even when there is totally uncalled for.
I just have to modify slightly the input: <code>let puzzleInput = @[<input>]</code>
and actually I do not even need to give it a nim extension, you can include
a file with any extension:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">include</span> <span class="hljs-string">"2021/input06.txt"</span>
<span class="hljs-keyword">echo</span> puzzleInput.len
<span class="hljs-keyword">echo</span> puzzleInput[<span class="hljs-number">0</span> ..< <span class="hljs-number">10</span>], <span class="hljs-string">" ... "</span>, puzzleInput[^<span class="hljs-number">10</span> .. ^<span class="hljs-number">1</span>]</code></pre>
<pre><samp>300
@[4, 3, 3, 5, 4, 1, 2, 1, 3, 1] ... @[1, 1, 1, 1, 2, 1, 1, 1, 2, 1]
</samp></pre>
<p>We are giving a list of numbers (all between 1 and 5) and
they represent "ages" of fishes that evolve with following rules:</p>
<ul>
<li>age decrease by 1 every time step</li>
<li>after reaching 0 a fish cycles back to age 6 and spawns a new fish of age 8 (added at the end of the list)
the question for part 1 is:</li>
</ul>
<blockquote>
<p><em>How many lanternfish would there be after 80 days?</em></p>
</blockquote>
<p>First observation is the fact that there is no interaction between fishes,
so I can concentrate on the evolution of a single fish.
I started thinking about properites of a couple of functions $f, F$:</p>
<ol>
<li>
<p>$f(a, s, t)$: number of generated fish at time $t from a fish of age a at time s</p>
</li>
<li>
<p>$f(a, s, t) = f(a, 0, t - s)$: I should not really care about $s$.</p>
</li>
<li>
<p>$f(a + b, s, t) = f(a, s, t - b)$</p>
</li>
<li>
<p>$F(a_1, ..., a_N, t)$: tot number of gen fish at time $t$ from fishes of "ages" $a_1, ... a_N$ at time 0
(I need to compute $F(\mathrm{input}, 80)$)</p>
</li>
<li>
<p>$F(a_1, ..., a_N, t) = f(a1, 0, t) + ... + f(aN, 0, t)$
(this is the observation above that fish evolutions are independent)</p>
</li>
<li>
<p>if $b_1, ... b_5$ are numbers of fishes with ages equal to $1, ..., 5$, then
$F(a_1, ..., a_N, t) = b_1 \cdot f(1, 0, t) + ... + b_5 \cdot f(5, 0, t)$</p>
</li>
<li>
<p>using rule 3 above I only need to compute $f(1, 0, t - i)$ for $i \in 1 .. 5$</p>
</li>
</ol>
<p>In practice I need only to compute the evolution of <code>@[1]</code>.
The basic function that ticks evolution at every time step:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> tick(school: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]): <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>] =
<span class="hljs-literal">result</span> = newSeqWith(len=school.len): -<span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> i, a <span class="hljs-keyword">in</span> school:
<span class="hljs-keyword">if</span> a == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>[i] = <span class="hljs-number">6</span>
<span class="hljs-literal">result</span>.add <span class="hljs-number">8</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>[i] = a - <span class="hljs-number">1</span></code></pre>
<p>and now solution for part 1</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> part1(input: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>], targetTime=<span class="hljs-number">80</span>): (<span class="hljs-built_in">int</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]) =
<span class="hljs-keyword">var</span>
counts: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
values: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> input:
<span class="hljs-keyword">assert</span> a <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
inc counts[a]
<span class="hljs-keyword">var</span> school = @[<span class="hljs-number">1</span>]
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. targetTime:
school = tick school
<span class="hljs-keyword">if</span> t >= targetTime - <span class="hljs-number">4</span>:
<span class="hljs-keyword">let</span> i = targetTime - t + <span class="hljs-number">1</span>
<span class="hljs-keyword">assert</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
values[i] = school.len
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-literal">result</span>[<span class="hljs-number">0</span>] += counts[i]*values[i]
<span class="hljs-literal">result</span>[<span class="hljs-number">1</span>] = school
<span class="hljs-keyword">let</span> testInput = @[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
doAssert part1(testInput)[<span class="hljs-number">0</span>] == <span class="hljs-number">5934</span>
dump part1(puzzleInput)[<span class="hljs-number">0</span>]</code></pre>
<pre><samp>part1(puzzleInput)[0] = 387413
</samp></pre>
<blockquote>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</blockquote>
<h3>Part 2</h3>
<p>And indeed we are asked to do the compute the same function
but over a horizon of 256 days. I tried expecting no result
and indeed the above approach does not give me an answer in
a reasonable time not even for test input.</p>
<p>Now the approach of counts in part1 was very promising
and I could have gotten a simple solution out of it
(see below of examples) but somehow I decided seeing 256
that I would chunk the problem in 16 chunks of duration 16
(making things more complicated than they should be).</p>
<p>So I set myself to compute:</p>
<ul>
<li>$g(i, t) = [g_0(i, t), ... g_8(i, t)]$</li>
<li>where $g(i, t)$ is a multi valued function that tells me
how many fishes of age $j$ I will have at time $t$
starting from a single fish of age $i$ ($g_j(i, t)$)</li>
</ul>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> g(chunkTime: <span class="hljs-built_in">int</span>): <span class="hljs-built_in">array</span>[<span class="hljs-number">9</span>, <span class="hljs-built_in">array</span>[<span class="hljs-number">9</span>, <span class="hljs-built_in">int</span>]] =
<span class="hljs-keyword">var</span>
school: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
school = @[i]
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. chunkTime:
school = tick school
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> school:
inc <span class="hljs-literal">result</span>[i][j]
<span class="hljs-keyword">let</span> chunk = g(<span class="hljs-number">16</span>)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"i: "</span>, i, <span class="hljs-string">"; results: "</span>, chunk[i]</code></pre>
<pre><samp>i: 0; results: [2, 0, 1, 0, 0, 1, 0, 1, 0]
i: 1; results: [0, 2, 0, 1, 0, 0, 1, 0, 1]
i: 2; results: [1, 0, 2, 0, 1, 0, 0, 0, 0]
i: 3; results: [0, 1, 0, 2, 0, 1, 0, 0, 0]
i: 4; results: [0, 0, 1, 0, 2, 0, 1, 0, 0]
i: 5; results: [0, 0, 0, 1, 0, 2, 0, 1, 0]
i: 6; results: [0, 0, 0, 0, 1, 0, 2, 0, 1]
i: 7; results: [1, 0, 0, 0, 0, 1, 0, 1, 0]
i: 8; results: [0, 1, 0, 0, 0, 0, 1, 0, 1]
</samp></pre>
<p>and here is the function that puts together the chunks:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> part2(input: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>], chunk=<span class="hljs-number">16</span>, times=<span class="hljs-number">16</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
counts: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
countsNext: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> input:
<span class="hljs-keyword">assert</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
inc counts[i]
<span class="hljs-keyword">let</span> chunkCounts = g(chunk)
<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. times:
countsNext = [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
countsNext[j] += counts[i]*chunkCounts[i][j]
counts = countsNext
<span class="hljs-literal">result</span> = sum(counts)
dump part2(testInput)
dump part2(puzzleInput)</code></pre>
<pre><samp>part2(testInput) = 26984457539
part2(puzzleInput) = 1738377086345
</samp></pre>
<blockquote>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</blockquote>
<h3>Highlights</h3>
<ul>
<li>see how simple could have been the solution if I went with the count approach
looking at <a href="https://github.com/PMunch/aoc2021/blob/master/day06/part2.nim">pmunch solution for part 2</a>.
Do not forget that he also <a href="https://www.youtube.com/playlist?list=PL9Yd0XwsGAqyd_lKcHm3f_nlE8vMdacV0">streams</a> his way about the solutions
which is a great way to see how someone reasons starting from scratch!</li>
<li>another approach for this would have been a recursive, memoized approach
like the one beautifully and concisely expressed by <a href="https://github.com/MichalMarsalek/Advent-of-code/blob/master/2021/Nim/day6.nim">MichalMarsalek</a></li>
<li>Michal got <a href="https://xkcd.com/356/">nerd sniped</a> by this and did a <a href="https://www.reddit.com/r/adventofcode/comments/ra3f5i/2021_day_6_part_3_day_googol/">part 3</a> and a <a href="https://www.reddit.com/r/adventofcode/comments/ra88up/2021_day_6_part_4_day_googolplex/">part 4</a>
with increasingly higher target times that requires more sophisticated behaviours
and even involve a nice little cryptographic primitive called the <a href="https://en.wikipedia.org/wiki/Linear-feedback_shift_register">LSFR</a> (Linear-feedback shift register).
It will take me a moment to absorb all that!</li>
<li>a <a href="https://www.reddit.com/r/adventofcode/comments/ra7gl8/2021_day_6_number_of_lanternfish_of_each_age_per/">nice animation</a> of the count of fishes by age on a log axis;
you can kind of see the exponential growth since it seems to grow linearly
on the log axis.</li>
<li>love the <a href="https://www.reddit.com/r/adventofcode/comments/rall1t/2021_day_6_the_lanternfish_solved_it_for_me/">ascii fish</a> as cursor writing the solution and what's the name of
the thing where you change indentation of code to make it look like
sort of image (in this case a tree branching down)?</li>
</ul>
<h3>Visualization</h3>
<p>I do not really know when I will have time do that (if ever), but I would
love to try and make a little <a href="https://github.com/ftsf/nico">nico</a> game with lanternfishes popping
around and your submarine trying to evoke whales to kill them...</p>
</main>
<footer>
<hr>
<div class="nb-box">
<span><span class="nb-small">made with <a href="https://pietroppeter.github.io/nimib/">nimib π³</a></span></span>
<span></span>
<span><button class="nb-small" id="show" onclick="toggleSourceDisplay()">Show Source</button></span>
</div>
</footer>
<section id="source">
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> animu, nimib
nbInit(theme=useAdventOfNim)
nb.useLatex
<span class="hljs-comment">#[
todo: add in nimib a `nbHeader "## Day 6: Lanternfish π£π"
that creats a header with link name day-6-lanternfish
and could add a headers section in context with...
well I am just picking a different api for the toc example of the cheatsheet...
]#</span>
nbText: <span class="hljs-string">"""## Day 6: [Lanternfish] π£π
[Lanternfish]: https://adventofcode.com/2021/day/6
Today is the classical case of: "simple (brute force) approach
will work for part1 but it will not work for part2. Also it
is the kind of the day where I tend to try and derive mathematical
properties on some functions and get lost along the way
(like [last year](../2020/day10hints.html)).
This year these days I have some catch up to do
(still have to post day 4 and 5, lots of viz missing)
so I will go with the first stuff that comes to mind,
the end result is not great so you might want to skip
to the end where I give links to better solutions by advent-of-nim-mers.
### Part 1
If you are still here, well,
I grew fond of the include trick that I used in [day02](day02.html)
that I am using it even when there is totally uncalled for.
I just have to modify slightly the input: `let puzzleInput = @[<input>]`
and actually I do not even need to give it a nim extension, you can include
a file with any extension:
"""</span>
nbCode:
<span class="hljs-keyword">include</span> <span class="hljs-string">"2021/input06.txt"</span>
<span class="hljs-keyword">echo</span> puzzleInput.len
<span class="hljs-keyword">echo</span> puzzleInput[<span class="hljs-number">0</span> ..< <span class="hljs-number">10</span>], <span class="hljs-string">" ... "</span>, puzzleInput[^<span class="hljs-number">10</span> .. ^<span class="hljs-number">1</span>]
nbText: <span class="hljs-string">"""
We are giving a list of numbers (all between 1 and 5) and
they represent "ages" of fishes that evolve with following rules:
- age decrease by 1 every time step
- after reaching 0 a fish cycles back to age 6 and spawns a new fish of age 8 (added at the end of the list)
the question for part 1 is:
> _How many lanternfish would there be after 80 days?_
First observation is the fact that there is no interaction between fishes,
so I can concentrate on the evolution of a single fish.
I started thinking about properites of a couple of functions $f, F$:
1. $f(a, s, t)$: number of generated fish at time $t from a fish of age a at time s
2. $f(a, s, t) = f(a, 0, t - s)$: I should not really care about $s$.
3. $f(a + b, s, t) = f(a, s, t - b)$
4. $F(a_1, ..., a_N, t)$: tot number of gen fish at time $t$ from fishes of "ages" $a_1, ... a_N$ at time 0
(I need to compute $F(\mathrm{input}, 80)$)
5. $F(a_1, ..., a_N, t) = f(a1, 0, t) + ... + f(aN, 0, t)$
(this is the observation above that fish evolutions are independent)
6. if $b_1, ... b_5$ are numbers of fishes with ages equal to $1, ..., 5$, then
$F(a_1, ..., a_N, t) = b_1 \cdot f(1, 0, t) + ... + b_5 \cdot f(5, 0, t)$
7. using rule 3 above I only need to compute $f(1, 0, t - i)$ for $i \in 1 .. 5$
In practice I need only to compute the evolution of `@[1]`.
The basic function that ticks evolution at every time step:
"""</span>
nbCode:
<span class="hljs-keyword">func</span> tick(school: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]): <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>] =
<span class="hljs-literal">result</span> = newSeqWith(len=school.len): -<span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> i, a <span class="hljs-keyword">in</span> school:
<span class="hljs-keyword">if</span> a == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>[i] = <span class="hljs-number">6</span>
<span class="hljs-literal">result</span>.add <span class="hljs-number">8</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>[i] = a - <span class="hljs-number">1</span>
nbText: <span class="hljs-string">"and now solution for part 1"</span>
nbCode:
<span class="hljs-keyword">func</span> part1(input: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>], targetTime=<span class="hljs-number">80</span>): (<span class="hljs-built_in">int</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]) =
<span class="hljs-keyword">var</span>
counts: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
values: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> input:
<span class="hljs-keyword">assert</span> a <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
inc counts[a]
<span class="hljs-keyword">var</span> school = @[<span class="hljs-number">1</span>]
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. targetTime:
school = tick school
<span class="hljs-keyword">if</span> t >= targetTime - <span class="hljs-number">4</span>:
<span class="hljs-keyword">let</span> i = targetTime - t + <span class="hljs-number">1</span>
<span class="hljs-keyword">assert</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
values[i] = school.len
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-literal">result</span>[<span class="hljs-number">0</span>] += counts[i]*values[i]
<span class="hljs-literal">result</span>[<span class="hljs-number">1</span>] = school
<span class="hljs-keyword">let</span> testInput = @[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
doAssert part1(testInput)[<span class="hljs-number">0</span>] == <span class="hljs-number">5934</span>
dump part1(puzzleInput)[<span class="hljs-number">0</span>]
gotTheStar
nbText: <span class="hljs-string">"""### Part 2
And indeed we are asked to do the compute the same function
but over a horizon of 256 days. I tried expecting no result
and indeed the above approach does not give me an answer in
a reasonable time not even for test input.
Now the approach of counts in part1 was very promising
and I could have gotten a simple solution out of it
(see below of examples) but somehow I decided seeing 256
that I would chunk the problem in 16 chunks of duration 16
(making things more complicated than they should be).
So I set myself to compute:
- $g(i, t) = [g_0(i, t), ... g_8(i, t)]$
- where $g(i, t)$ is a multi valued function that tells me
how many fishes of age $j$ I will have at time $t$
starting from a single fish of age $i$ ($g_j(i, t)$)
"""</span>
nbCode:
<span class="hljs-keyword">func</span> g(chunkTime: <span class="hljs-built_in">int</span>): <span class="hljs-built_in">array</span>[<span class="hljs-number">9</span>, <span class="hljs-built_in">array</span>[<span class="hljs-number">9</span>, <span class="hljs-built_in">int</span>]] =
<span class="hljs-keyword">var</span>
school: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
school = @[i]
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. chunkTime:
school = tick school
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> school:
inc <span class="hljs-literal">result</span>[i][j]
<span class="hljs-keyword">let</span> chunk = g(<span class="hljs-number">16</span>)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"i: "</span>, i, <span class="hljs-string">"; results: "</span>, chunk[i]
nbText: <span class="hljs-string">"and here is the function that puts together the chunks:"</span>
nbCode:
<span class="hljs-keyword">func</span> part2(input: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>], chunk=<span class="hljs-number">16</span>, times=<span class="hljs-number">16</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
counts: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
countsNext: <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span> .. <span class="hljs-number">8</span>, <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> input:
<span class="hljs-keyword">assert</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>
inc counts[i]
<span class="hljs-keyword">let</span> chunkCounts = g(chunk)
<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. times:
countsNext = [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">8</span>:
countsNext[j] += counts[i]*chunkCounts[i][j]
counts = countsNext
<span class="hljs-literal">result</span> = sum(counts)
dump part2(testInput)
dump part2(puzzleInput)
gotTheStar
nbText: <span class="hljs-string">"""### Highlights
- see how simple could have been the solution if I went with the count approach
looking at [pmunch solution for part 2](https://github.com/PMunch/aoc2021/blob/master/day06/part2.nim).
Do not forget that he also [streams](https://www.youtube.com/playlist?list=PL9Yd0XwsGAqyd_lKcHm3f_nlE8vMdacV0) his way about the solutions
which is a great way to see how someone reasons starting from scratch!
- another approach for this would have been a recursive, memoized approach
like the one beautifully and concisely expressed by [MichalMarsalek](https://github.com/MichalMarsalek/Advent-of-code/blob/master/2021/Nim/day6.nim)
- Michal got [nerd sniped] by this and did a [part 3] and a [part 4]
with increasingly higher target times that requires more sophisticated behaviours
and even involve a nice little cryptographic primitive called the [LSFR] (Linear-feedback shift register).
It will take me a moment to absorb all that!
- a [nice animation] of the count of fishes by age on a log axis;
you can kind of see the exponential growth since it seems to grow linearly
on the log axis.
- love the [ascii fish] as cursor writing the solution and what's the name of
the thing where you change indentation of code to make it look like
sort of image (in this case a tree branching down)?
[ascii fish]: https://www.reddit.com/r/adventofcode/comments/rall1t/2021_day_6_the_lanternfish_solved_it_for_me/
[nice animation]: https://www.reddit.com/r/adventofcode/comments/ra7gl8/2021_day_6_number_of_lanternfish_of_each_age_per/
[nerd sniped]: https://xkcd.com/356/
[part 3]: https://www.reddit.com/r/adventofcode/comments/ra3f5i/2021_day_6_part_3_day_googol/
[part 4]: https://www.reddit.com/r/adventofcode/comments/ra88up/2021_day_6_part_4_day_googolplex/
[LSFR]: https://en.wikipedia.org/wiki/Linear-feedback_shift_register
### Visualization
I do not really know when I will have time do that (if ever), but I would
love to try and make a little [nico] game with lanternfishes popping
around and your submarine trying to evoke whales to kill them...
[nico]: https://github.com/ftsf/nico
"""</span>
nbSave</code></pre>
</section><script>
function toggleSourceDisplay() {
var btn = document.getElementById("show")
var source = document.getElementById("source");
if (btn.innerHTML=="Show Source") {
btn.innerHTML = "Hide Source";
source.style.display = "block";
} else {
btn.innerHTML = "Show Source";
source.style.display = "none";
}
}
</script></body>
</html>