-
Notifications
You must be signed in to change notification settings - Fork 1
/
day11.html
720 lines (666 loc) Β· 43.6 KB
/
day11.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day11.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>
<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>2020\day11.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>2020, Day 11: Seating System</h2>
<p>standard day with a grid. Highlights:</p>
<ul>
<li>a clean refactoring after part1 to solve part2</li>
<li>ran into an expected issue when copying a sequence inside a proc (fixed with Orc, see: <a href="https://forum.nim-lang.org/t/7241">forum</a>)</li>
</ul>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span>
strutils, nimoji
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc):
<span class="hljs-keyword">echo</span> <span class="hljs-string">"using Orc :japanese_ogre:"</span>.emojize</code></pre>
<pre><samp>using Orc πΉ</samp></pre>
<p>First we parse and show the example grid.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">SeatGridKind</span> = <span class="hljs-keyword">enum</span>
sgFloor = <span class="hljs-string">"."</span>, sgEmpty = <span class="hljs-string">"L"</span>, sgOccupied = <span class="hljs-string">"#"</span>
<span class="hljs-type">SeatGrid</span> = <span class="hljs-keyword">object</span>
data: <span class="hljs-built_in">seq</span>[<span class="hljs-type">SeatGridKind</span>]
ncols: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""</span>.replace(
<span class="hljs-string">"</span><span class="hljs-meta">\c</span><span class="hljs-string">"</span>, <span class="hljs-string">""</span>)
<span class="hljs-keyword">proc</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">SeatGrid</span> =
<span class="hljs-keyword">for</span> i, c <span class="hljs-keyword">in</span> text:
<span class="hljs-keyword">case</span> c
<span class="hljs-keyword">of</span> <span class="hljs-string">'.'</span>:
<span class="hljs-literal">result</span>.data.add sgFloor
<span class="hljs-keyword">of</span> <span class="hljs-string">'L'</span>:
<span class="hljs-literal">result</span>.data.add sgEmpty
<span class="hljs-keyword">of</span> <span class="hljs-string">'\n'</span>:
<span class="hljs-keyword">if</span> <span class="hljs-literal">result</span>.ncols == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>.ncols = <span class="hljs-literal">result</span>.data.len
<span class="hljs-keyword">elif</span> <span class="hljs-literal">result</span>.data.len <span class="hljs-keyword">mod</span> <span class="hljs-literal">result</span>.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR expect same number of columns "</span>, i, <span class="hljs-string">" "</span>, c
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR cannot parse "</span>, i, <span class="hljs-string">" "</span>, c
<span class="hljs-keyword">proc</span> `$`(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-keyword">for</span> i, d <span class="hljs-keyword">in</span> g.data:
<span class="hljs-literal">result</span>.add:
<span class="hljs-keyword">case</span> d
<span class="hljs-keyword">of</span> sgFloor:
<span class="hljs-string">"."</span>
<span class="hljs-keyword">of</span> sgEmpty:
<span class="hljs-string">"L"</span>
<span class="hljs-keyword">of</span> sgOccupied:
<span class="hljs-string">"#"</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">var</span> exGrid = parse example
<span class="hljs-keyword">echo</span> exGrid
<span class="hljs-keyword">func</span> nrows(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
g.data.len <span class="hljs-keyword">div</span> g.ncols
<span class="hljs-keyword">echo</span> exGrid.nrows</code></pre>
<pre><samp>L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL
10</samp></pre>
<p>now we define a function to get adjacent indices. In part 2 we refactored this to use a <code>dir</code> function:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(part1):
<span class="hljs-keyword">iterator</span> adjIdx(i: <span class="hljs-built_in">int</span>; g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">if</span> i >= g.ncols:
<span class="hljs-keyword">yield</span> i - g.ncols <span class="hljs-comment">## up</span>
<span class="hljs-keyword">if</span> i >= g.ncols <span class="hljs-keyword">and</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">yield</span> i - g.ncols + <span class="hljs-number">1</span> <span class="hljs-comment">## up-right</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">yield</span> i + <span class="hljs-number">1</span> <span class="hljs-comment">## right</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> (i + g.ncols) < g.data.len:
<span class="hljs-keyword">yield</span> i + g.ncols + <span class="hljs-number">1</span> <span class="hljs-comment">## right-down</span>
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len:
<span class="hljs-keyword">yield</span> i + g.ncols <span class="hljs-comment">## down</span>
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len <span class="hljs-keyword">and</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">yield</span> i + g.ncols - <span class="hljs-number">1</span> <span class="hljs-comment">## down-left</span>
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">yield</span> i - <span class="hljs-number">1</span> <span class="hljs-comment">## left</span>
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> i >= g.ncols:
<span class="hljs-keyword">yield</span> i - g.ncols - <span class="hljs-number">1</span> <span class="hljs-comment">## left-up</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">type</span>
<span class="hljs-type">Direction</span> = <span class="hljs-keyword">enum</span>
up, upright, right, rightdown, down, downleft, left, leftup
<span class="hljs-keyword">func</span> dir(i: <span class="hljs-built_in">int</span>; d: <span class="hljs-type">Direction</span>; g: <span class="hljs-type">SeatGrid</span>): (<span class="hljs-built_in">bool</span>, <span class="hljs-built_in">int</span>) =
<span class="hljs-literal">result</span> = (<span class="hljs-literal">false</span>, i)
<span class="hljs-keyword">case</span> d
<span class="hljs-keyword">of</span> up:
<span class="hljs-keyword">if</span> i >= g.ncols:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols) <span class="hljs-comment">## up</span>
<span class="hljs-keyword">of</span> upright:
<span class="hljs-keyword">if</span> i >= g.ncols <span class="hljs-keyword">and</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols + <span class="hljs-number">1</span>) <span class="hljs-comment">## up-right</span>
<span class="hljs-keyword">of</span> right:
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + <span class="hljs-number">1</span>) <span class="hljs-comment">## right</span>
<span class="hljs-keyword">of</span> rightdown:
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> (i + g.ncols) < g.data.len:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols + <span class="hljs-number">1</span>) <span class="hljs-comment">## right-down</span>
<span class="hljs-keyword">of</span> down:
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols) <span class="hljs-comment">## down</span>
<span class="hljs-keyword">of</span> downleft:
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len <span class="hljs-keyword">and</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols - <span class="hljs-number">1</span>) <span class="hljs-comment">## down-left</span>
<span class="hljs-keyword">of</span> left:
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - <span class="hljs-number">1</span>) <span class="hljs-comment">## left</span>
<span class="hljs-keyword">of</span> leftup:
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> i >= g.ncols:
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols - <span class="hljs-number">1</span>) <span class="hljs-comment">## left-up</span>
<span class="hljs-keyword">iterator</span> adjIdx(i: <span class="hljs-built_in">int</span>; g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span> r = (<span class="hljs-literal">false</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-type">Direction</span>:
r = dir(i, d, g)
<span class="hljs-keyword">if</span> r[<span class="hljs-number">0</span>]:
<span class="hljs-keyword">yield</span> r[<span class="hljs-number">1</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">40</span>, <span class="hljs-number">45</span>, <span class="hljs-number">49</span>, <span class="hljs-number">90</span>, <span class="hljs-number">95</span>, <span class="hljs-number">99</span>]:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"adjIdx "</span>, i
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> adjIdx(i, exGrid):
<span class="hljs-keyword">echo</span> <span class="hljs-string">" "</span>, j</code></pre>
<pre><samp>adjIdx 0
1
11
10
adjIdx 5
6
16
15
14
4
adjIdx 9
19
18
8
adjIdx 40
30
31
41
51
50
adjIdx 45
35
36
46
56
55
54
44
34
adjIdx 49
39
59
58
48
38
adjIdx 90
80
81
91
adjIdx 95
85
86
96
94
84
adjIdx 99
89
98
88</samp></pre>
<p>a <code>step</code> proc to solve part1. this is where I ran in the <em>issue</em></p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> step(g: <span class="hljs-keyword">var</span> <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc):
<span class="hljs-keyword">let</span> data = g.data <span class="hljs-comment">## snapshot of grid</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">var</span> data = g.data
<span class="hljs-keyword">var</span> numOcc: <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> .. g.data.<span class="hljs-keyword">high</span>:
numOcc = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> adjIdx(i, g):
<span class="hljs-keyword">if</span> data[j] == sgOccupied:
inc numOcc
<span class="hljs-keyword">if</span> data[i] == sgEmpty <span class="hljs-keyword">and</span> numOcc == <span class="hljs-number">0</span>:
g.data[i] = sgOccupied
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">elif</span> data[i] == sgOccupied <span class="hljs-keyword">and</span> numOcc >= <span class="hljs-number">4</span>:
g.data[i] = sgEmpty
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">func</span> countOcc(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> g.data:
<span class="hljs-keyword">if</span> s == sgOccupied:
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">var</span> i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step(exGrid) > <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"step: "</span>, i
<span class="hljs-keyword">echo</span> exGrid
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(exGrid) <span class="hljs-comment">## 37</span>
<span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input11.txt"</span>.readFile.replace(<span class="hljs-string">"</span><span class="hljs-meta">\c</span><span class="hljs-string">"</span>, <span class="hljs-string">""</span>)
<span class="hljs-keyword">var</span> inpGrid = parse input
<span class="hljs-keyword">echo</span> (inpGrid.ncols, inpGrid.nrows)
i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step(inpGrid) > <span class="hljs-number">0</span>:
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"stops at step: "</span>, i <span class="hljs-comment">## 98</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(inpGrid) <span class="hljs-comment">## 2334</span></code></pre>
<pre><samp>step: 1
#.##.##.##
#######.##
#.#.#..#..
####.##.##
#.##.##.##
#.#####.##
..#.#.....
##########
#.######.#
#.#####.##
step: 2
#.LL.L#.##
#LLLLLL.L#
L.L.L..L..
#LLL.LL.L#
#.LL.LL.LL
#.LLLL#.##
..L.L.....
#LLLLLLLL#
#.LLLLLL.L
#.#LLLL.##
step: 3
#.##.L#.##
#L###LL.L#
L.#.#..#..
#L##.##.L#
#.##.LL.LL
#.###L#.##
..#.#.....
#L######L#
#.LL###L.L
#.#L###.##
step: 4
#.#L.L#.##
#LLL#LL.L#
L.L.L..#..
#LLL.##.L#
#.LL.LL.LL
#.LL#L#.##
..L.L.....
#L#LLLL#L#
#.LLLLLL.L
#.#L#L#.##
step: 5
#.#L.L#.##
#LLL#LL.L#
L.#.L..#..
#L##.##.L#
#.#L.LL.LL
#.#L#L#.##
..L.L.....
#L#L##L#L#
#.LLLLLL.L
#.#L#L#.##
numOcc: 37
(97, 92)
stops at step: 98
numOcc: 2334</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<p>and a <code>step2</code> proc to solve part2</p>
<pre><code class="nim hljs"><span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">defined</span>(part1):
<span class="hljs-keyword">echo</span> <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-meta">---Part2---\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">proc</span> step2(g: <span class="hljs-keyword">var</span> <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc):
<span class="hljs-keyword">let</span> data = g.data <span class="hljs-comment">## snapshot of grid</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">var</span> data = g.data
<span class="hljs-keyword">var</span> numOcc: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">var</span> r = (<span class="hljs-literal">false</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> .. g.data.<span class="hljs-keyword">high</span>:
numOcc = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-type">Direction</span>:
r = dir(i, d, g)
<span class="hljs-keyword">while</span> r[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> data[r[<span class="hljs-number">1</span>]] == sgFloor:
r = dir(r[<span class="hljs-number">1</span>], d, g)
<span class="hljs-keyword">if</span> r[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> data[r[<span class="hljs-number">1</span>]] == sgOccupied:
inc numOcc
<span class="hljs-keyword">if</span> data[i] == sgEmpty <span class="hljs-keyword">and</span> numOcc == <span class="hljs-number">0</span>:
g.data[i] = sgOccupied
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">elif</span> data[i] == sgOccupied <span class="hljs-keyword">and</span> numOcc >= <span class="hljs-number">5</span>:
g.data[i] = sgEmpty
inc <span class="hljs-literal">result</span>
i = <span class="hljs-number">1</span>
exGrid = parse example
<span class="hljs-keyword">while</span> step2(exGrid) > <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"step: "</span>, i
<span class="hljs-keyword">echo</span> exGrid
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(exGrid) <span class="hljs-comment">## 26</span>
inpGrid = parse input
i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step2(inpGrid) > <span class="hljs-number">0</span>:
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"stops at step: "</span>, i <span class="hljs-comment">## 86</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(inpGrid) <span class="hljs-comment">## 2100</span></code></pre>
<pre><samp>---Part2---
step: 1
#.##.##.##
#######.##
#.#.#..#..
####.##.##
#.##.##.##
#.#####.##
..#.#.....
##########
#.######.#
#.#####.##
step: 2
#.LL.LL.L#
#LLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLL#
#.LLLLLL.L
#.LLLLL.L#
step: 3
#.L#.##.L#
#L#####.LL
L.#.#..#..
##L#.##.##
#.##.#L.##
#.#####.#L
..#.#.....
LLL####LL#
#.L#####.L
#.L####.L#
step: 4
#.L#.L#.L#
#LLLLLL.LL
L.L.L..#..
##LL.LL.L#
L.LL.LL.L#
#.LLLLL.LL
..L.L.....
LLLLLLLLL#
#.LLLLL#.L
#.L#LL#.L#
step: 5
#.L#.L#.L#
#LLLLLL.LL
L.L.L..#..
##L#.#L.L#
L.L#.#L.L#
#.L####.LL
..#.#.....
LLL###LLL#
#.LLLLL#.L
#.L#LL#.L#
step: 6
#.L#.L#.L#
#LLLLLL.LL
L.L.L..#..
##L#.#L.L#
L.L#.LL.L#
#.LLLL#.LL
..#.L.....
LLL###LLL#
#.LLLLL#.L
#.L#LL#.L#
numOcc: 26
stops at step: 86
numOcc: 2100</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</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> nimib, animu
nbInit
nbText: <span class="hljs-string">"## 2020, Day 11: Seating System"</span>
nbText: <span class="hljs-string">"""standard day with a grid. Highlights:
* a clean refactoring after part1 to solve part2
* ran into an expected issue when copying a sequence inside a proc (fixed with Orc, see: [forum](https://forum.nim-lang.org/t/7241))
"""</span>
nbCode:
<span class="hljs-keyword">import</span> strutils, nimoji
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc):
<span class="hljs-keyword">echo</span> <span class="hljs-string">"using Orc :japanese_ogre:"</span>.emojize
nbText: <span class="hljs-string">"First we parse and show the example grid."</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">SeatGridKind</span> = <span class="hljs-keyword">enum</span>
sgFloor = <span class="hljs-string">"."</span>, sgEmpty = <span class="hljs-string">"L"</span>, sgOccupied = <span class="hljs-string">"#"</span>
<span class="hljs-type">SeatGrid</span> = <span class="hljs-keyword">object</span>
data: <span class="hljs-built_in">seq</span>[<span class="hljs-type">SeatGridKind</span>]
ncols: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""</span>.replace(<span class="hljs-string">"</span><span class="hljs-meta">\r</span><span class="hljs-string">"</span>, <span class="hljs-string">""</span>) <span class="hljs-comment"># I have had issues before (with the input)</span>
<span class="hljs-keyword">proc</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">SeatGrid</span> =
<span class="hljs-keyword">for</span> i, c <span class="hljs-keyword">in</span> text:
<span class="hljs-keyword">case</span> c:
<span class="hljs-keyword">of</span> <span class="hljs-string">'.'</span>:
<span class="hljs-literal">result</span>.data.add sgFloor
<span class="hljs-keyword">of</span> <span class="hljs-string">'L'</span>:
<span class="hljs-literal">result</span>.data.add sgEmpty
<span class="hljs-keyword">of</span> <span class="hljs-string">'\n'</span>:
<span class="hljs-keyword">if</span> <span class="hljs-literal">result</span>.ncols == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>.ncols = <span class="hljs-literal">result</span>.data.len
<span class="hljs-keyword">elif</span> <span class="hljs-literal">result</span>.data.len <span class="hljs-keyword">mod</span> <span class="hljs-literal">result</span>.ncols != <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR expect same number of columns "</span>, i, <span class="hljs-string">" "</span>, c
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR cannot parse "</span>, i, <span class="hljs-string">" "</span>, c
<span class="hljs-keyword">proc</span> `$`(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-keyword">for</span> i, d <span class="hljs-keyword">in</span> g.data:
<span class="hljs-literal">result</span>.add:
<span class="hljs-keyword">case</span> d:
<span class="hljs-keyword">of</span> sgFloor:
<span class="hljs-string">"."</span>
<span class="hljs-keyword">of</span> sgEmpty:
<span class="hljs-string">"L"</span>
<span class="hljs-keyword">of</span> sgOccupied:
<span class="hljs-string">"#"</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols == <span class="hljs-number">0</span>:
<span class="hljs-literal">result</span>.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">var</span> exGrid = parse example
<span class="hljs-keyword">echo</span> exGrid
<span class="hljs-keyword">func</span> nrows(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
g.data.len <span class="hljs-keyword">div</span> g.ncols
<span class="hljs-keyword">echo</span> exGrid.nrows <span class="hljs-comment"># 10</span>
nbText: <span class="hljs-string">"now we define a function to get adjacent indices. In part 2 we refactored this to use a `dir` function:"</span>
nbCode:
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(part1):
<span class="hljs-keyword">iterator</span> adjIdx(i: <span class="hljs-built_in">int</span>, g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">if</span> i >= g.ncols: <span class="hljs-comment">## not first row</span>
<span class="hljs-keyword">yield</span> i - g.ncols <span class="hljs-comment">## up</span>
<span class="hljs-keyword">if</span> i >= g.ncols <span class="hljs-keyword">and</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## .. and not last column</span>
<span class="hljs-keyword">yield</span> i - g.ncols + <span class="hljs-number">1</span> <span class="hljs-comment">## up-right</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## not last column</span>
<span class="hljs-keyword">yield</span> i + <span class="hljs-number">1</span> <span class="hljs-comment">## right</span>
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> (i + g.ncols) < g.data.len: <span class="hljs-comment">## .. and not last row</span>
<span class="hljs-keyword">yield</span> i + g.ncols + <span class="hljs-number">1</span> <span class="hljs-comment">## right-down</span>
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len: <span class="hljs-comment">## not last row</span>
<span class="hljs-keyword">yield</span> i + g.ncols <span class="hljs-comment">## down</span>
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len <span class="hljs-keyword">and</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## .. and not first column</span>
<span class="hljs-keyword">yield</span> i + g.ncols - <span class="hljs-number">1</span> <span class="hljs-comment">## down-left</span>
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## not first column</span>
<span class="hljs-keyword">yield</span> i - <span class="hljs-number">1</span> <span class="hljs-comment">## left</span>
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> i >= g.ncols: <span class="hljs-comment">## .. and not first row</span>
<span class="hljs-keyword">yield</span> i - g.ncols - <span class="hljs-number">1</span> <span class="hljs-comment">## left-up</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-comment"># refactor adjIdx for part2</span>
<span class="hljs-keyword">type</span> <span class="hljs-type">Direction</span> = <span class="hljs-keyword">enum</span>
up, upright, right, rightdown, down, downleft, left, leftup
<span class="hljs-keyword">func</span> dir(i: <span class="hljs-built_in">int</span>, d: <span class="hljs-type">Direction</span>, g: <span class="hljs-type">SeatGrid</span>): (<span class="hljs-built_in">bool</span>, <span class="hljs-built_in">int</span>) =
<span class="hljs-literal">result</span> = (<span class="hljs-literal">false</span>, i)
<span class="hljs-keyword">case</span> d:
<span class="hljs-keyword">of</span> up:
<span class="hljs-keyword">if</span> i >= g.ncols: <span class="hljs-comment">## not first row</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols) <span class="hljs-comment">## up</span>
<span class="hljs-keyword">of</span> upright:
<span class="hljs-keyword">if</span> i >= g.ncols <span class="hljs-keyword">and</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## .. and not last column</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols + <span class="hljs-number">1</span>) <span class="hljs-comment">## up-right</span>
<span class="hljs-keyword">of</span> right:
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## not last column</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + <span class="hljs-number">1</span> ) <span class="hljs-comment">## right</span>
<span class="hljs-keyword">of</span> rightdown:
<span class="hljs-keyword">if</span> (i + <span class="hljs-number">1</span>) <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> (i + g.ncols) < g.data.len: <span class="hljs-comment">## .. and not last row</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols + <span class="hljs-number">1</span>) <span class="hljs-comment">## right-down</span>
<span class="hljs-keyword">of</span> down:
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len: <span class="hljs-comment">## not last row</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols) <span class="hljs-comment">## down</span>
<span class="hljs-keyword">of</span> downleft:
<span class="hljs-keyword">if</span> (i + g.ncols) < g.data.len <span class="hljs-keyword">and</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## .. and not first column</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i + g.ncols - <span class="hljs-number">1</span>) <span class="hljs-comment">## down-left</span>
<span class="hljs-keyword">of</span> left:
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span>: <span class="hljs-comment">## not first column</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - <span class="hljs-number">1</span>) <span class="hljs-comment">## left</span>
<span class="hljs-keyword">of</span> leftup:
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">mod</span> g.ncols != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> i >= g.ncols: <span class="hljs-comment">## .. and not first row</span>
<span class="hljs-literal">result</span> = (<span class="hljs-literal">true</span>, i - g.ncols - <span class="hljs-number">1</span>) <span class="hljs-comment">## left-up</span>
<span class="hljs-keyword">iterator</span> adjIdx(i: <span class="hljs-built_in">int</span>, g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span> r = (<span class="hljs-literal">false</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-type">Direction</span>:
r = dir(i, d, g)
<span class="hljs-keyword">if</span> r[<span class="hljs-number">0</span>]:
<span class="hljs-keyword">yield</span> r[<span class="hljs-number">1</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">40</span>, <span class="hljs-number">45</span>, <span class="hljs-number">49</span>, <span class="hljs-number">90</span>, <span class="hljs-number">95</span>, <span class="hljs-number">99</span>]:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"adjIdx "</span>, i
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> adjIdx(i, exGrid):
<span class="hljs-keyword">echo</span> <span class="hljs-string">" "</span>, j
nbText: <span class="hljs-string">"a `step` proc to solve part1. this is where I ran in the _issue_"</span>
nbCode:
<span class="hljs-keyword">proc</span> step(g: <span class="hljs-keyword">var</span> <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc): <span class="hljs-comment">## let data would not work with gcRefc see: https://forum.nim-lang.org/t/7241</span>
<span class="hljs-keyword">let</span> data = g.data <span class="hljs-comment">## snapshot of grid</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">var</span> data = g.data
<span class="hljs-keyword">var</span> numOcc: <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> .. g.data.<span class="hljs-keyword">high</span>:
numOcc = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> adjIdx(i, g):
<span class="hljs-keyword">if</span> data[j] == sgOccupied:
inc numOcc
<span class="hljs-keyword">if</span> data[i] == sgEmpty <span class="hljs-keyword">and</span> numOcc == <span class="hljs-number">0</span>:
g.data[i] = sgOccupied
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">elif</span> data[i] == sgOccupied <span class="hljs-keyword">and</span> numOcc >= <span class="hljs-number">4</span>:
g.data[i] = sgEmpty
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">func</span> countOcc(g: <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> g.data:
<span class="hljs-keyword">if</span> s == sgOccupied:
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">var</span> i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step(exGrid) > <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"step: "</span>, i
<span class="hljs-keyword">echo</span> exGrid
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(exGrid) <span class="hljs-comment">## 37</span>
<span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input11.txt"</span>.readFile.replace(<span class="hljs-string">"</span><span class="hljs-meta">\r</span><span class="hljs-string">"</span>, <span class="hljs-string">""</span>)
<span class="hljs-keyword">var</span> inpGrid = parse input
<span class="hljs-keyword">echo</span> (inpGrid.ncols, inpGrid.nrows)
i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step(inpGrid) > <span class="hljs-number">0</span>:
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"stops at step: "</span>, i <span class="hljs-comment">## 98</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(inpGrid) <span class="hljs-comment">## 2334</span>
gotTheStar
nbText: <span class="hljs-string">"and a `step2` proc to solve part2"</span>
nbCode:
<span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">defined</span>(part1):
<span class="hljs-keyword">echo</span> <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-meta">---Part2---\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">proc</span> step2(g: <span class="hljs-keyword">var</span> <span class="hljs-type">SeatGrid</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">when</span> <span class="hljs-keyword">defined</span>(gcOrc): <span class="hljs-comment">## let data would not work with gcRefc see: https://forum.nim-lang.org/t/7241</span>
<span class="hljs-keyword">let</span> data = g.data <span class="hljs-comment">## snapshot of grid</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">var</span> data = g.data
<span class="hljs-keyword">var</span> numOcc: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">var</span> r = (<span class="hljs-literal">false</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> .. g.data.<span class="hljs-keyword">high</span>:
numOcc = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-type">Direction</span>:
r = dir(i, d, g)
<span class="hljs-keyword">while</span> r[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> data[r[<span class="hljs-number">1</span>]] == sgFloor:
r = dir(r[<span class="hljs-number">1</span>], d, g)
<span class="hljs-keyword">if</span> r[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> data[r[<span class="hljs-number">1</span>]] == sgOccupied:
inc numOcc
<span class="hljs-keyword">if</span> data[i] == sgEmpty <span class="hljs-keyword">and</span> numOcc == <span class="hljs-number">0</span>:
g.data[i] = sgOccupied
inc <span class="hljs-literal">result</span>
<span class="hljs-keyword">elif</span> data[i] == sgOccupied <span class="hljs-keyword">and</span> numOcc >= <span class="hljs-number">5</span>:
g.data[i] = sgEmpty
inc <span class="hljs-literal">result</span>
i = <span class="hljs-number">1</span>
exGrid = parse example
<span class="hljs-keyword">while</span> step2(exGrid) > <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"step: "</span>, i
<span class="hljs-keyword">echo</span> exGrid
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(exGrid) <span class="hljs-comment">## 26</span>
inpGrid = parse input
i = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> step2(inpGrid) > <span class="hljs-number">0</span>:
inc i
<span class="hljs-keyword">echo</span> <span class="hljs-string">"stops at step: "</span>, i <span class="hljs-comment">## 86</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"numOcc: "</span>, countOcc(inpGrid) <span class="hljs-comment">## 2100</span>
gotTheStar
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>