-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16notes.txt
635 lines (602 loc) · 8.2 KB
/
day16notes.txt
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
# Part 1
Ah, binary protocol decoding, nice!
Thinking of the primitives I'm going to need. Would a `get-bit` that
returns the next bit from the input be sufficient?
Or should I load an entire line's worth of data in a buffer, and then
carve sub-buffers from that?
Oh, the input contains a single line!
I'm going to assume that padding bits only occur at the end of the
outermost packet for now. The text hints at this:
> The three unlabeled 0 bits at the end are extra due to the hexadecimal
> representation and should be ignored.
I think I'm going to try with `get-bit`. How should it indicate the end of
input? Returning `ff` should be OK; in any case if all goes well we should
never request a bit past the end, I think. I'll just add a breakpoint there.
I wonder if literal values can be numbers longer than16 bits… Interestingly,
if there are longer numbers and my 16-bit storage overflows, it shouldn't
matter for part 1.
Note that my input is 1398 bytes long.
If there is indeed no extra padding inside packets, I should be able to
ignore type 0 length, right? Oh no, because I don't know how many packets
to parse inside.
On the other hand, part 1 only asks me to sum up version numbers, so I don't
actually need to respect the hierarchy. I'll go ahead and consider operator
packets done after their length, which should be equivalent.
Ah, but then I won't know when to stop reading, of course. Hmm, so I really
need a stack of open packets. Let's just recurse for now.
Done in 107 minutes, yay.
# Part 2
As expected, operators now have functions. This should be quite straightforward
to implement, the only wrinkle being how to separate sub-packet enumeration
(by length or by count) from the operation itself.
Oh, and hopefully we won't overflow. Let's just dump the hierarchy of my input
to see if I have to expect anything.
Versions can be completely skipped over for part 2.
Here's my complete tree:
+ | 42188 + 27726 + …
* | 0
< | 0
58459
42678
56547
* | 0
< | 0
+ | 36
13
15
8
+ | 18
6
9
3
24725
*
=
1598
1058
36758
*
<
+
9
15
14
+
2
15
4
110
M
88
M
26623
58753
130
m
31859
30294
15
22255
74
*
21
111
248
120
154
M
10
9
1319
597
+
+
+
M
+
M
m
+
M
M
m
m
*
*
+
m
*
m
*
m
46691
*
172
203
201
12
*
150
4
246
156
161
*
235
66
*
>
+
14
4
5
+
8
10
14
59423
14
*
55319
>
25
41065
+
119
19890
31921
39323
22863
*
30372
>
62287
29811
238
+ | 27726
27726
6
*
>
3888
3888
2783
+
*
11
5
3
*
15
8
13
*
6
12
9
*
38
<
80
15627
M | 42188
42188
1462
m
5337
m
13
117
2
*
17345
=
+
13
11
8
+
2
14
6
M | 64111
10838
64111
20723
8
12119
40843
191
3
m
30
3155
+
7
33
+
55676
50399
75
13987
*
1697
<
56166
87
* | 0
63239
> | 0
30147
45376
+
34387
11
4819
*
49
259
1917
*
57015
>
1798
568
*
+
15
8
4
+
5
7
14
+
10
14
7
m
220
2504
34369
1363
*
>
10391
10391
61360
*
47053
<
12326
1
*
58973
=
96
200
*
=
36216
36216
5
*
57249
>
+
11
5
12
+
10
9
6
*
<
3132
3132
5043
*
158
<
16633
16633
The largest literal is 64111, but it is used in a max operator, so it doesn't immediately overflow.
Likewise, the next largest is 63239, which is immediately multiplied by zero. It almost looks
as if the input has been constructed so that intermediate results would fit in 16 bits unsigned
integers… alas, at least the top-level sum overflows since it includes 42188 and 27726.
Looks like I'll have to implement a 64-bit integer stack.
"Your answer is too low"…
I just spent a few minutes reimplementing the evaluator in awk+dc to have confirmation:
$ ./uxnrun.sh day16.tal <(echo D8005AC2A8F0) |awk 'BEGIN { print "[f] sf [1+]
so [0*] sz [1+]so [ sa sb 0 la lb <o ]s< [ sa sb 0 la lb >o ]s> [ sa sb 0 la
lb =o ]s= [ * ]s* [ + ]s+ [r]sr [ sa sb la lb la lb >r sZ ]sM [ sa sb la lb la
lb <r sZ ]sm";} /\|/{printf "[%s\n]P\n", $0; op=substr($0,1,1); if(op=="#"){}
else if(op!=" "){print "l" op "x";}else{print substr($0,1,22);}; print
"f"}'|dc 2>&1
Loaded day16.rom
5 | 5
5
15 | 5 15
15
5
< | 1
1
Running on my full input, `dc` gives the exact same (wrong) answer as my code. This is
frustrating.
But I just realized how stupid I've been earlier. "It almost looks as if the
input has been constructed so that intermediate results would fit in 16 bits
unsigned integers", indeed… of course if I truncate the integers as I read them,
it's no surprise that none will be above 65535!
This should be a relatively quick fix.
Here's the (hopefully) correct tree:
+
*
<
58459
1025718
56547
*
<
+
13
15
8
+
6
9
3
24725
*
=
1598
1058
9342870
*
<
+
9
15
14
+
2
15
4
110
M
88
M
550911
58753
130
m
1184267379
30294
15
22255
74
*
21
111
248
120
154
M
10
9
1319
597
+
+
+
M
+
M
m
+
M
M
m
m
*
*
+
m
*
m
*
m
46691
*
172
203
201
12
*
150
4
246
156
161
*
235
66
*
>
+
14
4
5
+
8
10
14
2975066143
14
*
62398191639
>
25
237673
+
119
19890
13925553
32725178779
219471
*
882340
>
62287
57243825267
238
+
552014
6
*
>
3888
3888
2783
+
*
11
5
3
*
15
8
13
*
6
12
9
*
38
<
80
15627
M
1737543754956
1462
m
5337
m
13
117
2
*
9782209
=
+
13
11
8
+
2
14
6
M
207446
2357871
20723
8
36509658967
43384020875
191
3
m
30
3091795027
+
7
33
+
150395260
121095391
75
13987
*
1697
<
231267174
87
*
158787303175
>
554435
45376
+
523233691219
11
201427
*
49
259
1917
*
57015
>
246023942
568
*
+
15
8
4
+
5
7
14
+
10
14
7
m
220
2504
362049
1363
*
>
640297111
640297111
61360
*
102283213
<
12326
1
*
58973
=
96
200
*
=
36216
36216
5
*
199221153
>
+
11
5
12
+
10
9
6
*
<
3132
3132
37819315
*
158
<
17013692973305
17013692973305
Plenty of non-16-bit values here!
Running the computation again thankfully gives the right answer.