-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.roc
644 lines (497 loc) · 15.7 KB
/
Day17.roc
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
interface Day17 exposes [ output ] imports [ ListZip, TestUtil ]
output : List I64 -> List (List I64)
output = \puzzleInput ->
testCube = parseCube 3 3 testInput
puzzleCube = parseCube 8 8 puzzleInput
testC4 = parseC4 3 3 testInput
puzzleC4 = parseC4 8 8 puzzleInput
[ TestUtil.verify 17 1 1 (firstResult testCube ) 112
, TestUtil.show 17 1 (firstResult puzzleCube)
, TestUtil.verify 17 2 1 (secondResult testC4 ) 848
, TestUtil.show 17 2 (secondResult puzzleC4)
]
# first part
firstResult : Cube -> I64
firstResult = \cube ->
bufSum (repeatCycles cube 6)
repeatCycles : Cube, I64 -> Cube
repeatCycles = \cube, cnt ->
if cnt > 0 then
repeatCycles (cycle cube) (cnt - 1)
else
cube
cycle : Cube -> Cube
cycle = \cube ->
xl = safeGet cube 2
yl = safeGet cube 7
zl = safeGet cube 12
newCube = empty (xl + 2) (yl + 2) (zl + 2) 0
xa = safeGet cube 4
ya = safeGet cube 9
za = safeGet cube 14
xMin = xa - 1
yMin = ya - 1
zMin = za - 1
xMax = xa + xl
yMax = ya + yl
zMax = za + zl
cycleHelper cube newCube xMin xMax xMin yMin yMax yMin zMin zMax zMin
cycleHelper : Cube, Cube, I64, I64, I64, I64, I64, I64, I64, I64, I64 -> Cube
cycleHelper = \oldCube, cube, xMin, xMax, x, yMin, yMax, y, zMin, zMax, z ->
newCube =
if val oldCube x y z -1 -1 -1 0 0 > 0 then
setIp cube (x - xMin + 1) (y - yMin + 1) (z - zMin + 1) 1
else
cube
if z < zMax then
cycleHelper oldCube newCube xMin xMax x yMin yMax y zMin zMax (z + 1)
else if y < yMax then
cycleHelper oldCube newCube xMin xMax x yMin yMax (y + 1) zMin zMax zMin
else if x < xMax then
cycleHelper oldCube newCube xMin xMax (x + 1) yMin yMax yMin zMin zMax zMin
else
newCube
val : Cube, I64, I64, I64, I64, I64, I64, I64, I64 -> I64
val = \cube, x, y, z, xd, yd, zd, v, n ->
if xd == 0 && yd == 0 && zd == 0 then
newV = get cube x y z
val cube x y z 0 0 1 newV n
else
newN = n + get cube (x + xd) (y + yd) (z + zd)
if zd < 1 then
val cube x y z xd yd (zd + 1) v newN
else if yd < 1 then
val cube x y z xd (yd + 1) -1 v newN
else if xd < 1 then
val cube x y z (xd + 1) -1 -1 v newN
else if (v == 1 && newN == 2) || newN == 3 then
1
else
0
Cube : List I64
#{ dv : I64
#, xc : I64, xl : I64, xo : I64, xa : I64, xe : I64
#, yc : I64, yl : I64, yo : I64, ya : I64, ye : I64
#, zc : I64, zl : I64, zo : I64, za : I64, ze : I64
#}
empty : I64, I64, I64, I64 -> Cube
empty = \xc, yc, zc, dv ->
List.concat
(emptyCub xc yc zc dv)
(emptyBuf xc yc zc dv)
emptyCub : I64, I64, I64, I64 -> List I64
emptyCub = \xc, yc, zc, dv ->
[ dv, xc, 0, 0, 0, 0, yc, 0, 0, 0, 0, zc, 0, 0, 0, 0 ]
emptyBuf : I64, I64, I64, I64 -> List I64
emptyBuf = \xc, yc, zc, dv ->
List.repeat (xc * yc * zc) dv
#cubeClr : Cube a, I64, I64, I64 -> Cube a
#cubeClr = \cube, _x, _y, _z ->
# # todo
# cube
copy : Cube -> Cube
copy = \cube ->
List.join [ cube ]
set : Cube, I64, I64, I64, I64 -> Cube
set = \cube, x, y, z, v ->
cube |> copy |> setIp x y z v
setIp : Cube, I64, I64, I64, I64 -> Cube
setIp = \cube, x, y, z, v ->
cube
|> setCub x y z
|> setBuf x y z v
setCub : Cube, I64, I64, I64 -> Cube
setCub = \cube, x, y, z ->
xl = safeGet cube 2
yl = safeGet cube 7
zl = safeGet cube 12
if xl > 0 then
xc = safeGet cube 1
yc = safeGet cube 6
zc = safeGet cube 11
xa = safeGet cube 4
ya = safeGet cube 9
za = safeGet cube 14
xd = x - xa
yd = y - ya
zd = z - za
if xl - xd > xc || xd >= xc || yl - yd > yc || yd >= yc || zl - zd > zc || zd >= zc then
cube
|> List.set 5 x
|> List.set 10 y
|> List.set 15 z
else if xd < 0 || xd >= xl || yd < 0 || yd >= yl || zd < 0 || zd >= zl then
xo = safeGet cube 3
yo = safeGet cube 8
zo = safeGet cube 13
xp = xo + xd
yp = yo + yd
zp = zo + zd
cube
|> List.set 2 (if xd >= xl then xd + 1 else if xd >= 0 then xl else xl - xd)
|> List.set 7 (if yd >= yl then yd + 1 else if yd >= 0 then yl else yl - yd)
|> List.set 12 (if zd >= zl then zd + 1 else if zd >= 0 then zl else zl - zd)
|> List.set 3 (if xd >= 0 then xo else if xp >= 0 then xp else xp + xc )
|> List.set 8 (if yd >= 0 then yo else if yp >= 0 then yp else yp + yc )
|> List.set 13 (if zd >= 0 then zo else if zp >= 0 then zp else zp + zc )
|> List.set 4 (if xd >= 0 then xa else x)
|> List.set 9 (if yd >= 0 then ya else y)
|> List.set 14 (if zd >= 0 then za else z)
else
cube
else
cube
|> List.set 2 1
|> List.set 7 1
|> List.set 12 1
|> List.set 4 x
|> List.set 9 y
|> List.set 14 z
setBuf : Cube, I64, I64, I64, I64 -> Cube
setBuf = \cube, x, y, z, v ->
pos = bufPos cube x y z
List.set cube pos v
get : Cube, I64, I64, I64 -> I64
get = \cube, x, y, z ->
pos = bufPos cube x y z
if pos < 0 then
safeGet cube 0
else
when List.get cube pos is
Ok v ->
v
_ ->
safeGet cube 0
bufPos : Cube, I64, I64, I64 -> I64
bufPos = \cube, x, y, z ->
xa = safeGet cube 4
ya = safeGet cube 9
za = safeGet cube 14
xd = x - xa
yd = y - ya
zd = z - za
if xd < 0 || yd < 0 || zd < 0 then
-1
else
xl = safeGet cube 2
yl = safeGet cube 7
zl = safeGet cube 12
if xd >= xl || yd >= yl || zd >= zl then
-1
else
xc = safeGet cube 1
yc = safeGet cube 6
zc = safeGet cube 11
xo = safeGet cube 3
yo = safeGet cube 8
zo = safeGet cube 13
xp = xo + xd
yp = yo + yd
zp = zo + zd
((if xp >= xc then xp - xc else xp) * yc +
(if yp >= yc then yp - yc else yp)) * zc +
(if zp >= zc then zp - zc else zp) + 16
sum : Cube -> I64
sum = \cube ->
List.sum cube
cubSum : Cube -> I64
cubSum = \cube ->
safeGet cube 0 +
safeGet cube 1 +
safeGet cube 2 +
safeGet cube 3 +
safeGet cube 4 +
safeGet cube 5 +
safeGet cube 6 +
safeGet cube 7 +
safeGet cube 8 +
safeGet cube 9 +
safeGet cube 10 +
safeGet cube 11 +
safeGet cube 12 +
safeGet cube 13 +
safeGet cube 14 +
safeGet cube 15
bufSum : Cube -> I64
bufSum = \cube ->
sum cube - cubSum cube
# second part
secondResult : C4 -> I64
secondResult = \c4 ->
sumB4 (repeatCycles4 c4 6)
repeatCycles4 : C4, I64 -> C4
repeatCycles4 = \c4, cnt ->
if cnt > 0 then
repeatCycles4 (cycle4 c4) (cnt - 1)
else
c4
cycle4 : C4 -> C4
cycle4 = \c4 ->
wl = safeGet c4 2
xl = safeGet c4 7
yl = safeGet c4 12
zl = safeGet c4 17
newC4 = empty4 (wl + 2) (xl + 2) (yl + 2) (zl + 2) 0
wa = safeGet c4 4
xa = safeGet c4 9
ya = safeGet c4 14
za = safeGet c4 19
wMin = wa - 1
xMin = xa - 1
yMin = ya - 1
zMin = za - 1
wMax = wa + wl
xMax = xa + xl
yMax = ya + yl
zMax = za + zl
cycle4Helper c4 newC4 wMin wMax wMin xMin xMax xMin yMin yMax yMin zMin zMax zMin
cycle4Helper : C4, C4, I64, I64, I64, I64, I64, I64, I64, I64, I64, I64, I64, I64 -> C4
cycle4Helper = \oldC4, c4, wMin, wMax, w, xMin, xMax, x, yMin, yMax, y, zMin, zMax, z ->
newC4 =
if val4 oldC4 w x y z -1 -1 -1 -1 0 0 > 0 then
setIp4 c4 (w - wMin + 1) (x - xMin + 1) (y - yMin + 1) (z - zMin + 1) 1
else
c4
if z < zMax then
cycle4Helper oldC4 newC4 wMin wMax w xMin xMax x yMin yMax y zMin zMax (z + 1)
else if y < yMax then
cycle4Helper oldC4 newC4 wMin wMax w xMin xMax x yMin yMax (y + 1) zMin zMax zMin
else if x < xMax then
cycle4Helper oldC4 newC4 wMin wMax w xMin xMax (x + 1) yMin yMax yMin zMin zMax zMin
else if w < wMax then
cycle4Helper oldC4 newC4 wMin wMax (w + 1) xMin xMax xMin yMin yMax yMin zMin zMax zMin
else
newC4
val4 : C4, I64, I64, I64, I64, I64, I64, I64, I64, I64, I64 -> I64
val4 = \c4, w, x, y, z, wd, xd, yd, zd, v, n ->
if wd == 0 && xd == 0 && yd == 0 && zd == 0 then
newV = get4 c4 w x y z
val4 c4 w x y z 0 0 0 1 newV n
else
newN = n + get4 c4 (w + wd) (x + xd) (y + yd) (z + zd)
if zd < 1 then
val4 c4 w x y z wd xd yd (zd + 1) v newN
else if yd < 1 then
val4 c4 w x y z wd xd (yd + 1) -1 v newN
else if xd < 1 then
val4 c4 w x y z wd (xd + 1) -1 -1 v newN
else if wd < 1 then
val4 c4 w x y z (wd + 1) -1 -1 -1 v newN
else if (v == 1 && newN == 2) || newN == 3 then
1
else
0
C4 : List I64
empty4 : I64, I64, I64, I64, I64 -> Cube
empty4 = \wc, xc, yc, zc, dv ->
List.concat
(emptyC4 wc xc yc zc dv)
(emptyB4 wc xc yc zc dv)
emptyC4 : I64, I64, I64, I64, I64 -> List I64
emptyC4 = \wc, xc, yc, zc, dv ->
[ dv, wc, 0, 0, 0, 0, xc, 0, 0, 0, 0, yc, 0, 0, 0, 0, zc, 0, 0, 0, 0 ]
emptyB4 : I64, I64, I64, I64, I64 -> List I64
emptyB4 = \wc, xc, yc, zc, dv ->
List.repeat (wc * xc * yc * zc) dv
copy4 : C4 -> C4
copy4 = \c4 ->
List.join [ c4 ]
set4 : C4, I64, I64, I64, I64, I64 -> C4
set4 = \c4, w, x, y, z, v ->
c4 |> copy4 |> setIp4 w x y z v
setIp4 : C4, I64, I64, I64, I64, I64 -> C4
setIp4 = \c4, w, x, y, z, v ->
c4
|> setC4 w x y z
|> setB4 w x y z v
setC4 : C4, I64, I64, I64, I64 -> C4
setC4 = \c4, w, x, y, z ->
wl = safeGet c4 2
xl = safeGet c4 7
yl = safeGet c4 12
zl = safeGet c4 17
if wl > 0 then
wc = safeGet c4 1
xc = safeGet c4 6
yc = safeGet c4 11
zc = safeGet c4 16
wa = safeGet c4 4
xa = safeGet c4 9
ya = safeGet c4 14
za = safeGet c4 19
wd = w - wa
xd = x - xa
yd = y - ya
zd = z - za
if wl - wd > wc || wd >= wc || xl - xd > xc || xd >= xc || yl - yd > yc || yd >= yc || zl - zd > zc || zd >= zc then
c4
|> List.set 5 w
|> List.set 10 x
|> List.set 15 y
|> List.set 20 z
else if wd < 0 || wd >= wl || xd < 0 || xd >= xl || yd < 0 || yd >= yl || zd < 0 || zd >= zl then
wo = safeGet c4 3
xo = safeGet c4 8
yo = safeGet c4 13
zo = safeGet c4 18
wp = wo + wd
xp = xo + xd
yp = yo + yd
zp = zo + zd
c4
|> List.set 2 (if wd >= wl then wd + 1 else if wd >= 0 then wl else wl - wd)
|> List.set 7 (if xd >= xl then xd + 1 else if xd >= 0 then xl else xl - xd)
|> List.set 12 (if yd >= yl then yd + 1 else if yd >= 0 then yl else yl - yd)
|> List.set 17 (if zd >= zl then zd + 1 else if zd >= 0 then zl else zl - zd)
|> List.set 3 (if wd >= 0 then wo else if wp >= 0 then wp else wp + wc )
|> List.set 8 (if xd >= 0 then xo else if xp >= 0 then xp else xp + xc )
|> List.set 13 (if yd >= 0 then yo else if yp >= 0 then yp else yp + yc )
|> List.set 18 (if zd >= 0 then zo else if zp >= 0 then zp else zp + zc )
|> List.set 4 (if wd >= 0 then wa else w)
|> List.set 9 (if xd >= 0 then xa else x)
|> List.set 14 (if yd >= 0 then ya else y)
|> List.set 19 (if zd >= 0 then za else z)
else
c4
else
c4
|> List.set 2 1
|> List.set 7 1
|> List.set 12 1
|> List.set 17 1
|> List.set 4 w
|> List.set 9 x
|> List.set 14 y
|> List.set 19 z
setB4 : C4, I64, I64, I64, I64, I64 -> C4
setB4 = \c4, w, x, y, z, v ->
pos = posB4 c4 w x y z
List.set c4 pos v
get4 : C4, I64, I64, I64, I64 -> I64
get4 = \c4, w, x, y, z ->
pos = posB4 c4 w x y z
if pos < 0 then
safeGet c4 0
else
when List.get c4 pos is
Ok v ->
v
_ ->
safeGet c4 0
posB4 : C4, I64, I64, I64, I64 -> I64
posB4 = \c4, w, x, y, z ->
wa = safeGet c4 4
xa = safeGet c4 9
ya = safeGet c4 14
za = safeGet c4 19
wd = w - wa
xd = x - xa
yd = y - ya
zd = z - za
if wd < 0 || xd < 0 || yd < 0 || zd < 0 then
-1
else
wl = safeGet c4 2
xl = safeGet c4 7
yl = safeGet c4 12
zl = safeGet c4 17
if wd >= wl || xd >= xl || yd >= yl || zd >= zl then
-1
else
wc = safeGet c4 1
xc = safeGet c4 6
yc = safeGet c4 11
zc = safeGet c4 16
wo = safeGet c4 3
xo = safeGet c4 8
yo = safeGet c4 13
zo = safeGet c4 18
wp = wo + wd
xp = xo + xd
yp = yo + yd
zp = zo + zd
(((if wp >= wc then wp - wc else wp) * xc +
(if xp >= xc then xp - xc else xp)) * yc +
(if yp >= yc then yp - yc else yp)) * zc +
(if zp >= zc then zp - zc else zp) + 21
sum4 : C4 -> I64
sum4 = \c4 ->
List.sum c4
sumC4 : C4 -> I64
sumC4 = \c4 ->
safeGet c4 0 +
safeGet c4 1 +
safeGet c4 2 +
safeGet c4 3 +
safeGet c4 4 +
safeGet c4 5 +
safeGet c4 6 +
safeGet c4 7 +
safeGet c4 8 +
safeGet c4 9 +
safeGet c4 10 +
safeGet c4 11 +
safeGet c4 12 +
safeGet c4 13 +
safeGet c4 14 +
safeGet c4 15 +
safeGet c4 16 +
safeGet c4 17 +
safeGet c4 18 +
safeGet c4 19 +
safeGet c4 20
sumB4 : C4 -> I64
sumB4 = \c4 ->
sum4 c4 - sumC4 c4
# utils
safeGet : Cube, I64 -> I64
safeGet = \cube, idx ->
when List.get cube idx is
Ok v -> v
_ -> -1
# parser
parseCube : I64, I64, List I64 -> Cube
parseCube = \yc, zc, input ->
cube = empty 1 yc zc 0
zip = ListZip.newAtFirst input 0
parseCubeHelper zip input cube 1 1
parseCubeHelper : ListZip.Zip, List I64, Cube, I64, I64 -> Cube
parseCubeHelper = \zip, input, cube, y, z ->
if ListZip.afterLast zip then
cube
else
newZip = ListZip.forward zip input
when zip.val is
35 ->
newCube = set cube 1 y z 1
parseCubeHelper newZip input newCube y (z + 1)
46 ->
parseCubeHelper newZip input cube y (z + 1)
_ ->
parseCubeHelper newZip input cube (y + 1) 1
parseC4 : I64, I64, List I64 -> C4
parseC4 = \yc, zc, input ->
c4 = empty4 1 1 yc zc 0
zip = ListZip.newAtFirst input 0
parseC4Helper zip input c4 1 1
parseC4Helper : ListZip.Zip, List I64, C4, I64, I64 -> C4
parseC4Helper = \zip, input, c4, y, z ->
if ListZip.afterLast zip then
c4
else
newZip = ListZip.forward zip input
when zip.val is
35 ->
newC4 = set4 c4 1 1 y z 1
parseC4Helper newZip input newC4 y (z + 1)
46 ->
parseC4Helper newZip input c4 y (z + 1)
_ ->
parseC4Helper newZip input c4 (y + 1) 1
# test data
testInput : List I64
testInput =
[ 46, 35, 46, 10
, 46, 46, 35, 10
, 35, 35, 35
]