Compare commits

...

26 Commits

Author SHA1 Message Date
bcb4358a5c day 24, interpreter working 2021-12-24 15:25:41 +01:00
5fce73d69e day 22 part 1 2021-12-22 23:13:11 +01:00
f7e797d920 day 21 part 2, got it!
I was overwriting created states in new-states while I should have been
cumulating.
2021-12-22 22:11:32 +01:00
db000226d7 day 21 part 2, WIP getting closer 2021-12-22 14:58:04 +01:00
4ed9b57aa9 day 21 part 2 WIP 2021-12-22 01:28:47 +01:00
aa77b51cd0 refactoring (probably needed for previous days too, oops) 2021-12-21 23:46:46 +01:00
089f2c8800 day 21 part 1 2021-12-21 23:45:42 +01:00
c2ce053037 proper day 20 2021-12-20 15:17:55 +01:00
596f326726 hey, re-use first two steps! 2021-12-20 13:24:26 +01:00
a7cb6fae09 day 20 (but I think I got lucky xD) 2021-12-20 11:50:35 +01:00
cfdb5bcc8e day 18 2021-12-18 23:37:33 +01:00
8e44c86134 day 16 2021-12-17 10:40:23 +01:00
5be5acbfbc day 14 2021-12-14 19:26:29 +01:00
7c3908c15c simpler 2021-12-13 23:16:50 +01:00
acf4a35309 shorter, using values and apply 2021-12-13 22:53:35 +01:00
69a73f90d7 day 13 2021-12-13 16:42:59 +01:00
ea9e5b7167 indentation 2021-12-12 20:37:37 +01:00
6a0df03604 day 12 part 2 2021-12-12 20:36:33 +01:00
7662c00cf9 day 12 part 1 2021-12-12 12:29:53 +01:00
0388455b85 moved get-lines to common.scm 2021-12-12 12:29:44 +01:00
bdc9004df6 use char->number from common in day 5 2021-12-11 20:19:34 +01:00
98336be2d7 day 11 part 2 2021-12-11 12:02:23 +01:00
a22420b554 day 11, part 1 2021-12-11 11:54:01 +01:00
d35640dc63 oops, import srfi 43 for common to compile 2021-12-11 11:53:50 +01:00
f0d18d31ce customize default return value of matrix-get when peeking outside 2021-12-11 10:49:04 +01:00
a1266ca648 factorized heightmap code into matrix code, and using records now 2021-12-11 10:30:24 +01:00
25 changed files with 2940 additions and 67 deletions

View File

@@ -1,17 +1,5 @@
(import (srfi :43)) ; vector extensions
; returns numbers 0 to 9 from ascii character
(define (char->number c)
(- (char->integer c) 48)) ; 48 is ASCII's number zero
; reads a numnber from input
; also consumes the next non-number character in the file!
(define (get-number file)
(let loop [(n 0)]
(let [(c (get-char file))]
(if (char-numeric? c)
[loop (+ (char->number c) (* n 10))]
n))))
(load "../common.scm")
; reads pairs of coordinates
; returns a list of the form (((x1 y1) (x2 y2))...)

View File

@@ -1,39 +1,6 @@
(import (srfi :43)) ; vector extensions
(load "../common.scm")
; constructor of heightmap object
; from a vector of vector of altitudes
(define (make-heightmap data)
(list
(vector-length (vector-ref data 0)) ; width
(vector-length data) ; height
data))
; heightmap object getters
(define (heightmap-width heightmap) (car heightmap))
(define (heightmap-height heightmap) (cadr heightmap))
(define (heightmap-data heightmap) (caddr heightmap))
(define (sample-heightmap heightmap x y)
(if (or (< x 0)
(< y 0)
(>= x (heightmap-width heightmap))
(>= y (heightmap-height heightmap)))
9
(vector-ref (vector-ref (heightmap-data heightmap) y) x)))
; parse altitude data from file and return an heightmap object
(define (load-heightmap file)
(let y-loop [(heightmap '())]
(let x-loop [(line '())]
(let [(c (get-char file))]
(cond
[(eof-object? c)
(make-heightmap (reverse-list->vector heightmap))]
[(char-whitespace? c)
(y-loop (cons (reverse-list->vector line) heightmap))]
[(char-numeric? c)
(x-loop (cons (char->number c) line))])))))
; constructor of low point objects
; from their position and altitude
(define (make-low-point x y altitude)
@@ -50,17 +17,17 @@
(lambda (y line)
(vector-for-each
(lambda (x altitude)
(let [(left (sample-heightmap heightmap (- x 1) y))
(right (sample-heightmap heightmap (+ x 1) y))
(up (sample-heightmap heightmap x (- y 1)))
(down (sample-heightmap heightmap x (+ y 1)))]
(let [(left (matrix-get heightmap (- x 1) y 9))
(right (matrix-get heightmap (+ x 1) y 9))
(up (matrix-get heightmap x (- y 1) 9))
(down (matrix-get heightmap x (+ y 1) 9))]
(when (and (< altitude left)
(< altitude right)
(< altitude up)
(< altitude down))
(set! low-points (cons (make-low-point x y altitude) low-points)))))
line))
(heightmap-data heightmap))
(matrix-data heightmap))
low-points))
; compute risk level from low point list
@@ -70,8 +37,8 @@
; starting with from a given low-point
; call closure with and all points of the corresponding basin
(define (visit-basin heightmap low-point closure)
(let* [(width (heightmap-width heightmap))
(height (heightmap-height heightmap))
(let* [(width (matrix-width heightmap))
(height (matrix-height heightmap))
(width-1 (- width 1))
(height-1 (- height 1))
(visited (make-vector (* width height) 0))]
@@ -82,7 +49,7 @@
(let [(ofs (offset x y))]
(when (= (vector-ref visited ofs) 0)
(vector-set! visited ofs 1)
(when (< (sample-heightmap heightmap x y) 9)
(when (< (matrix-get heightmap x y 9) 9)
(closure x y)
(when (> x 0) (flood-fill (- x 1) y))
(when (< x width-1) (flood-fill (+ x 1) y))
@@ -118,12 +85,12 @@
(lambda (file)
; write header to file: "P3" followed by width, height and max value of channel
(fprintf file "P3~%~a ~a~%255~%"
(heightmap-width heightmap)
(heightmap-height heightmap))
(matrix-width heightmap)
(matrix-height heightmap))
(let* [(width (heightmap-width heightmap))
(let* [(width (matrix-width heightmap))
(width*3 (* width 3))
(height (heightmap-height heightmap))
(height (matrix-height heightmap))
(data (make-vector (* width*3 width)))]
; from (x,y) returns of offset of pixel's red channel in data vector
@@ -136,7 +103,7 @@
(let y-loop [(y 0)]
(let x-loop [(x 0)]
(let [(ofs (offset x y))
(gray-level (* (sample-heightmap heightmap x y) 20))]
(gray-level (* (matrix-get heightmap x y 9) 20))]
; red channel
(vector-set! data ofs gray-level)
; green channel
@@ -173,7 +140,7 @@
(call-with-input-file
"input"
(lambda (file)
(let* [(heightmap (load-heightmap file))
(let* [(heightmap (load-matrix file))
(low-points (find-low-points heightmap))]
(printf "part 1:~% Risk level: ~a~%"
(risk-level low-points))

View File

@@ -1,9 +1,4 @@
; get list of lines from a file
(define (get-lines file)
(let loop [(lines '())]
(if (eof-object? (peek-char file))
lines
(loop (cons (get-line file) lines)))))
(load "../common.scm")
; check if character can close chunk
(define (closing? c)

72
11-dumbo-octopus/code.scm Normal file
View File

@@ -0,0 +1,72 @@
(load "../common.scm")
(define-record-type vec2i (fields x y))
; flash given octopus
; ie. increase energy of neighbor ones
; returns list of octopuses that now need to flash too
(define (flash! matrix x y)
(let [(extra '())]
(let y-loop [(yy (- y 1))]
(let x-loop [(xx (- x 1))]
(when (not (and (= xx x) (= yy y)))
(when (< (matrix-get matrix xx yy 0) 10)
(when (= (matrix-inc! matrix xx yy) 10)
(set! extra (cons (make-vec2i xx yy) extra)))))
(when (< xx (+ x 1))
(x-loop (+ xx 1))))
(when (< yy (+ y 1))
(y-loop (+ yy 1))))
extra))
(define (step! matrix)
(let [(flashes '())]
; increase energy of all octopuses
(update-matrix!
matrix
(lambda (x y energy)
(when (= energy 9)
(set! flashes (cons (make-vec2i x y) flashes)))
(+ 1 energy)))
; recursively flash octopuses
(let loop [(flash flashes)]
(when (not (null? flash))
(loop (append (cdr flash)
(flash! matrix (vec2i-x (car flash))
(vec2i-y (car flash)))))))
; drain all octopuses that flashed
(update-matrix!
matrix
(lambda (x y energy)
(if (< energy 10) energy 0)))
; count octopuses that flashed
(vector-fold
(lambda (y sum line)
(+ sum
(vector-fold
(lambda (x sum value)
(if (= value 0) (+ sum 1) sum))
0 line)))
0 (matrix-data matrix))))
(call-with-input-file
"input"
(lambda (file)
(let [(matrix (load-matrix file))
(flash-count 0)]
(let loop [(i 0)]
(set! flash-count (+ flash-count
(step! matrix)))
(when (< i 99)
(loop (+ i 1))))
(printf "part 1:~% ~a flashes after a hundred steps.~%"
flash-count)
(let loop [(i 100)]
(if (= (matrix-sum matrix) 0)
(printf "part 2:~% Octopuses synchronized at step ~a.~%" i)
(begin
(step! matrix)
(loop (+ i 1))))))))

10
11-dumbo-octopus/input Normal file
View File

@@ -0,0 +1,10 @@
1326253315
3427728113
5751612542
6543868322
4422526221
2234325647
1773174887
7281321674
6562513118
4824541522

108
12-passage-pathing/code.scm Normal file
View File

@@ -0,0 +1,108 @@
(load "../common.scm")
; graphs in this code are represented by hashtables of node names to list of node names
; splits string into two at first separator character encountered
; returns #f if there is given separator is not present
(define (split-string str separator)
(let [(len (string-length str))]
(let loop [(i 0)]
(if (= i len)
#f
(if (char=? (string-ref str i) separator)
(list (substring str 0 i)
(substring str (+ i 1) len))
(loop (+ i 1)))))))
; add a directed edge to given graph
(define (add-edge! nodes start end)
(hashtable-update! nodes start
(lambda (node)
(if (member end node)
node
(cons end node)))
'()))
; build graph from edges defined in a file
(define (make-graph file)
(let [(lines (get-lines file))
(nodes (make-hashtable string-hash string=? 20))]
(for-each
(lambda (line)
(let [(edge (split-string line #\-))]
(let [(start (car edge))
(end (cadr edge))]
(add-edge! nodes start end)
(add-edge! nodes end start))))
lines)
nodes))
; big caves are uppercase nodes
(define (is-big? node)
(char-upper-case? (string-ref node 0)))
; count how many obj there is in lst
(define (count-members obj lst)
(let count [(n 0) (lst lst)]
(let [(sub (member obj lst))]
(if sub
(count (+ n 1) (cdr sub))
n))))
; calls visitor for all possible paths in graph
; with max-small = 1, visits small caves at most once
; with max-small = 2, visits one small cave at most twice and others at most once
; with max-small >= 3 or 0, probably garbage
(define (visit-paths graph max-small visitor)
(let visit [(node "start")
(path '())
(max-small max-small)]
(if (string=? node "end")
(visitor (cons node path))
(for-each
(lambda (child)
(let [(is-big (is-big? child))]
(when (and (not (string=? child "start"))
(or is-big
(< (count-members child path) max-small)))
(visit child (cons node path)
(if (and (not is-big) (member child path)) 1 max-small)))))
(hashtable-ref graph node '())))))
; computes how many possible paths there are in graph
(define (count-paths graph max-small)
(let [(count 0)]
(visit-paths graph max-small
(lambda (path)
(set! count (+ 1 count))))
count))
; print all possible paths
(define (print-paths graph max-small)
(visit-paths graph max-small
(lambda (path)
(printf "~a~%" (reverse path)))))
;(let [(nodes (make-hashtable string-hash string=? 20))]
; (define (add-2! s e)
; (add-edge! nodes s e)
; (add-edge! nodes e s))
; (add-2! "start" "A")
; (add-2! "start" "b")
; (add-2! "A" "c")
; (add-2! "A" "b")
; (add-2! "b" "d")
; (add-2! "A" "end")
; (add-2! "b" "end")
; (printf "~a of them at 1 small~%" (count-paths nodes 1))
; (printf "~a of them at one 2 small~%" (count-paths nodes 2))
; (print-paths nodes 2))
(call-with-input-file
"input"
(lambda (file)
(let [(graph (make-graph file))]
(printf "part 1:~% ~a paths with at most 1 visit to small caves.~%"
(count-paths graph 1))
(printf "part 2:~% ~a paths with at most 2 visits to small caves.~%"
(count-paths graph 2)))))

22
12-passage-pathing/input Normal file
View File

@@ -0,0 +1,22 @@
zi-end
XR-start
zk-zi
TS-zk
zw-vl
zk-zw
end-po
ws-zw
TS-ws
po-TS
po-YH
po-xk
zi-ws
zk-end
zi-XR
XR-zk
vl-TS
start-zw
vl-start
XR-zw
XR-vl
XR-ws

View File

@@ -0,0 +1,69 @@
(load "../common.scm")
; reads a number from file up until the first non numeric character
(define (read-number file)
(let loop [(n 0)]
(let [(c (get-char file))]
(if (or (eof-object? c)
(not (char-numeric? c)))
n
(loop (+ (* n 10)
(char->number c)))))))
; parses pairs of numbers separated by one character
(define (read-coords file)
(let loop [(coords '())]
(if (char-numeric? (peek-char file))
(loop (cons (list (read-number file)
(read-number file))
coords))
coords)))
; parses lines of "fold along A=N" where A is a character or N is a number
(define (read-folds file)
(let loop [(folds '())]
(if (eof-object? (peek-char file))
(reverse folds)
(begin
(get-string-n file 11) ; consume "fold along "
(let [(axis (get-char file))
(position (begin
(get-char file) ; consume =
(read-number file)))]
(loop (cons (list axis position) folds)))))))
; mirrors coordinates along given axis and line/column
; returns a new list of coordinates
(define (fold coords axis-position)
(let-values [((axis position) (apply values axis-position))]
(let* [(position*2 (* position 2))
(mirror (if (char=? axis #\x)
(lambda (x y) (list (if (> x position) (- position*2 x) x) y))
(lambda (x y) (list x (if (> y position) (- position*2 y) y)))))
(transform (lambda (output coord)
(let [(mirrored (apply mirror coord))]
(if (member mirrored output) output (cons mirrored output)))))]
(fold-left transform '() coords))))
(call-with-input-file
"input"
(lambda (file)
(let* [(coords (read-coords file))
(folds (begin
(get-char file) ; consume newline
(read-folds file)))]
(let [(first-fold (fold coords (car folds)))]
(printf "part 1:~% ~a dots after first fold.~%"
(length first-fold)))
(let [(fully-folded (fold-left fold coords folds))]
(let [(width (+ 1 (fold-left (lambda (m c) (max m (car c))) 0 fully-folded)))
(height (+ 1 (fold-left (lambda (m c) (max m (cadr c))) 0 fully-folded)))]
(printf "part 2:~% Code is:~%")
(let y-loop [(y 0)]
(when (< y height)
(let x-loop [(x 0)]
(when (< x width)
(printf (if (member (list x y) fully-folded) "#" " "))
(x-loop (+ x 1))))
(printf "~%")
(y-loop (+ y 1)))))))))

View File

@@ -0,0 +1,789 @@
103,224
624,491
808,688
1076,130
700,26
55,794
119,724
773,809
875,33
922,135
509,260
801,176
1143,85
619,526
1250,138
753,431
1260,654
276,457
637,718
1183,115
284,137
539,757
279,85
1128,474
406,469
1086,170
927,673
310,702
35,796
268,892
202,249
820,878
992,121
339,649
1275,796
1113,290
336,616
522,471
755,764
631,204
241,393
455,546
1165,176
304,276
855,859
582,380
437,9
944,400
199,770
124,473
522,423
167,85
606,493
1158,688
1186,473
981,54
790,507
704,493
788,362
971,201
512,889
758,810
1101,770
325,840
734,873
725,586
416,105
1183,365
1233,725
698,256
418,112
520,507
276,773
634,185
483,693
612,256
651,603
1225,53
1268,759
35,339
311,474
984,592
1237,770
1093,129
755,639
671,278
788,471
430,810
693,231
281,227
454,178
1,54
1290,499
619,407
1081,575
1252,266
1186,421
698,638
1218,561
954,763
692,402
923,532
281,631
222,777
1047,316
1109,215
513,824
771,645
802,640
769,471
42,256
619,80
913,425
340,68
397,833
539,137
1136,654
515,18
507,247
1274,402
970,364
913,362
328,410
775,404
164,16
356,763
545,353
790,506
780,46
378,569
276,437
878,777
159,613
313,137
377,418
134,22
483,14
1207,670
1238,738
528,268
913,761
1009,501
1217,805
865,361
460,801
358,715
857,775
552,842
771,197
627,40
765,577
1071,704
862,638
811,686
219,413
904,425
728,738
1020,588
1240,225
619,78
611,180
610,474
388,135
957,582
194,43
1027,842
917,864
537,770
831,26
1240,669
1034,582
1210,49
1036,282
1217,537
271,100
537,691
584,290
224,618
1111,546
610,26
726,221
1053,509
1295,278
1094,859
725,245
552,52
981,168
448,190
905,64
700,126
132,179
930,648
692,189
659,291
1027,852
984,78
318,121
1026,757
889,385
184,596
746,50
850,93
233,173
234,130
201,679
290,588
510,0
284,869
939,628
147,303
1265,78
445,361
763,477
1250,586
1178,715
282,196
1290,617
719,191
460,93
932,325
529,343
112,829
967,567
43,714
545,541
541,423
612,759
1262,196
473,522
878,117
512,596
182,446
783,529
797,70
768,65
477,270
552,810
373,227
502,682
378,121
430,724
592,565
180,254
145,718
184,298
748,354
127,147
845,400
951,485
393,260
898,357
480,667
541,871
507,199
1267,714
407,406
288,494
152,654
827,14
1028,698
1000,192
549,169
490,211
1111,592
535,404
26,854
372,126
455,859
236,340
716,808
387,532
1101,361
857,119
999,420
276,878
373,417
923,670
92,729
179,703
619,555
455,348
668,266
674,217
1139,880
442,530
1042,2
594,586
890,354
13,757
698,759
268,677
1125,276
314,826
1210,889
207,42
691,78
241,277
42,638
176,82
445,891
315,649
1081,95
880,810
782,268
472,229
2,826
769,23
974,616
898,537
102,674
604,374
415,480
1154,453
1110,14
798,695
562,333
336,809
254,508
412,313
271,221
763,171
883,255
992,773
999,327
818,36
1081,215
974,278
1274,588
808,234
373,477
43,42
552,674
609,816
440,486
1081,679
105,723
281,667
865,96
574,442
290,140
1289,294
338,682
299,264
1268,638
1154,5
42,759
999,474
502,688
35,787
452,400
1116,679
571,684
420,781
1056,396
1309,467
1238,193
1287,204
448,638
418,844
832,82
1053,385
1290,395
1101,768
1027,42
1146,858
768,787
716,674
765,541
504,82
512,471
1206,565
691,80
391,764
1208,514
1,427
23,690
994,212
837,522
1265,750
209,320
952,715
999,687
329,726
1233,85
126,626
117,462
803,143
652,712
142,298
582,50
370,309
20,838
903,488
380,515
681,536
1128,446
291,75
502,206
70,673
233,721
127,81
99,255
765,765
102,871
388,51
612,135
763,723
387,425
639,278
1191,180
728,514
607,869
199,302
699,282
420,354
823,64
527,747
405,64
564,50
35,526
524,334
913,61
542,667
582,722
492,36
765,353
1074,340
840,450
875,400
937,477
2,264
1146,260
1027,714
1101,320
182,474
801,767
1260,381
72,253
195,73
390,497
1056,508
107,275
753,504
699,180
890,540
894,105
509,270
530,249
378,94
50,65
870,486
1029,667
728,840
974,809
803,751
92,354
749,509
1198,206
144,0
290,208
1195,249
470,647
1233,809
801,270
874,793
903,406
310,192
1109,551
806,812
1069,841
1161,158
1044,311
1168,23
542,227
1268,361
1166,446
421,385
782,432
478,82
1034,885
1154,217
216,859
677,497
372,660
806,82
612,704
483,201
820,260
933,418
873,885
744,789
147,143
594,220
45,144
1268,135
699,119
383,221
27,854
328,352
499,880
1298,75
437,317
102,52
1184,432
996,826
448,25
1211,764
987,165
913,133
35,595
271,436
1036,155
1255,624
1287,690
1298,714
492,688
827,693
1129,521
1028,196
1176,388
654,190
967,106
340,364
201,215
59,670
495,606
768,227
576,873
746,844
618,82
1238,253
387,84
50,325
631,690
705,418
937,711
1207,152
152,682
768,829
528,686
716,220
1183,141
277,866
35,487
44,749
798,889
705,476
326,816
1251,224
1310,381
1109,343
1211,255
60,138
1081,103
659,603
214,65
835,703
1290,838
534,645
539,249
1074,50
406,425
209,768
652,182
512,838
316,212
820,16
35,147
985,691
604,65
407,488
811,880
1193,432
937,675
344,416
400,325
1145,168
868,530
769,871
1298,404
134,388
1039,436
1033,812
681,393
517,337
582,844
725,201
1012,187
480,675
654,704
1275,144
115,249
12,588
1028,644
306,220
326,592
671,431
97,431
1146,16
380,648
955,686
604,325
119,842
1086,618
1309,54
562,540
1146,464
996,523
311,327
758,471
92,281
671,655
550,565
125,103
699,404
228,752
1298,371
576,649
552,380
1260,513
821,427
783,141
0,78
683,40
806,826
552,276
371,567
431,0
267,532
1086,808
336,278
372,234
530,46
58,628
12,371
855,546
282,644
938,660
1176,22
995,649
49,476
1238,514
1279,485
539,697
50,513
1190,256
490,235
430,84
18,217
453,215
1103,42
274,164
242,537
884,752
550,329
855,299
100,845
328,526
910,514
933,476
100,49
2,378
301,501
1298,180
939,842
470,298
35,528
746,498
156,453
857,439
564,554
144,777
1165,718
840,695
546,793
749,385
576,201
529,103
835,751
1266,26
1093,577
207,852
1297,757
933,866
841,724
1201,129
199,859
1074,560
335,649
271,794
1125,724
838,229
1183,779
782,686
413,393
765,129
77,725
1146,430
59,224
870,107
656,704
164,270
865,320
181,521
1063,40
229,95
142,392
987,515
838,644
276,9
103,670
790,58
174,240
1298,819
1131,703
435,861
1308,712
782,626
806,147
523,261
979,379
1026,137
591,191
726,540
92,561
400,392
1309,427
728,380
937,417
171,880
1308,182
1218,540
311,119
104,565
201,343
266,583
363,371
397,89
119,714
919,130
142,502
773,691
995,245
1265,816
748,540
1185,103
1072,789
185,170
1020,754
946,626
677,600
93,805
1081,791
633,294
512,840
728,50
771,137
520,506
897,393
1154,565
541,471
1297,137
967,474
499,208
639,879
318,269
629,281
975,649
490,260
545,577
156,5
383,669
913,532
119,164
162,688
1092,793
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

View File

@@ -0,0 +1,124 @@
(import (srfi :43)) ; vector extensions
; hash function for a list of two characters
(define (char-pair-hash cp)
(+ (* (char->integer (car cp)) 79)
(char->integer (cadr cp))))
; parses lines of "AB -> C" with A, B and C being characters
; returns hashtable where keys are ABs and values are Cs
(define (read-table file)
(let loop [(table (make-hashtable char-pair-hash equal? 32))]
(let [(line (get-line file))]
(if (eof-object? line)
table
(let [(key (list (string-ref line 0) (string-ref line 1)))
(value (string-ref line 6))]
(hashtable-set! table key value)
(loop table))))))
; generate new polymer from inserting elements in between each pair according to given table
(define (insert polymer table)
(let [(end (- (string-length polymer) 1))]
(let loop [(i 0) (output "")]
(if (< i end)
(loop (+ i 1)
(let* [(pair (list (string-ref polymer i) (string-ref polymer (+ i 1))))
(element (hashtable-ref table pair #f))]
(if element
(string-append output (string (car pair)) (string element))
(string-append output (string (car pair))))))
(string-append output (substring polymer end (+ end 1)))))))
; parameter counts is a vector where each entry is a list (C N)
; where C is a key and N is a number.
; returns a vector where entry of ele has increased by count.
(define (add-ele-count counts ele count)
(let [(index (vector-index (lambda (p) (equal? (car p) ele)) counts))]
(if index
(begin
(vector-set! counts index (list ele (+ count (cadr (vector-ref counts index)))))
counts)
(vector-append counts (vector (list ele count))))))
; count amount of each character in input string.
; returns vector of lists where car is a character and cadr is the amount of it in input
; sorted from most common to least common
(define (count-chars str)
(let [(count (vector))]
(string-for-each (lambda (c) (set! count (add-ele-count count c 1))) str)
count))
; print result to console
(define (display-subtracted-most-least-elements elements)
(let* [(elements (vector-sort (lambda (l r) (> (cadr l) (cadr r))) elements))
(most-common (vector-ref elements 0))
(least-common (vector-ref elements (- (vector-length elements) 1)))]
(printf "Most common ~a (~a) - least common ~a (~a) = ~a~%"
(car most-common) (cadr most-common)
(car least-common) (cadr least-common)
(- (cadr most-common) (cadr least-common)))))
; returns a vector counting the number of each consecutive characters in str
(define (count-pairs str)
(let [(len-1 (- (string-length str) 1))]
(let loop [(n 0) (pairs '#())]
(if (< n len-1)
(loop (+ n 1)
(let [(pair (list (string-ref str n) (string-ref str (+ n 1))))]
(add-ele-count pairs pair 1)))
pairs))))
; count the number of elements where given pairs-count is a vector
; with amounts of pairs of elements
; #(((a b) N) ((c d) N') ...)
(define (count-elements pairs-count)
(vector-fold
(lambda (index elements-count pair-count)
(let [(pair (car pair-count))
(count (cadr pair-count))]
(set! elements-count (add-ele-count elements-count (car pair) count))
(add-ele-count elements-count (cadr pair) count)))
'#() pairs-count))
; directly computes resulting element amounts of
; inserting elements in input polymer up to depth steps
; returns vector of lists where car is an element's character and cadr is the amount of it
(define (polymerize polymer table depth)
(let step [(d 0)
(polymer-pairs (count-pairs polymer))
(candidate-pairs '#())]
(if (< d depth)
(let [(new-polymer-pairs '#()) (new-candidates '#())]
(define (multiply index pair-count)
(let* [(pair (car pair-count))
(count (cadr pair-count))
(element (hashtable-ref table pair #f))]
(if element
(let [(new-pair (list (car pair) element))
(new-candidate (list element (cadr pair)))]
(set! new-polymer-pairs (add-ele-count new-polymer-pairs new-pair count))
(set! new-candidates (add-ele-count new-candidates new-candidate count)))
(add-ele-count pair count))))
(vector-for-each multiply polymer-pairs)
(vector-for-each multiply candidate-pairs)
(step (+ d 1) new-polymer-pairs new-candidates))
(let [(elements (count-elements polymer-pairs))
(last (string-ref polymer (- (string-length polymer) 1)))]
(add-ele-count elements last 1)))))
(call-with-input-file
"input"
(lambda (file)
(let* [(polymer (get-line file))
(table (begin
(get-line file) ; skip empty line
(read-table file)))]
(let loop [(n 0) (polymer polymer)]
(if (< n 10)
(loop (+ n 1) (insert polymer table))
(begin
(printf "part 1:~% After 10 polymerization steps: ~% ")
(display-subtracted-most-least-elements (count-chars polymer)))))
(printf "part 2:~% After 40 polymerization steps: ~% ")
(display-subtracted-most-least-elements (polymerize polymer table 40)))))

View File

@@ -0,0 +1,102 @@
OHFNNCKCVOBHSSHONBNF
SV -> O
KP -> H
FP -> B
VP -> V
KN -> S
KS -> O
SB -> K
BS -> K
OF -> O
ON -> S
VS -> F
CK -> C
FB -> K
CH -> K
HS -> H
PO -> F
NP -> N
FH -> C
FO -> O
FF -> C
CO -> K
NB -> V
PP -> S
BB -> N
HH -> B
KK -> H
OP -> K
OS -> V
KV -> F
VH -> F
OB -> S
CN -> H
SF -> K
SN -> P
NF -> H
HB -> V
VC -> S
PS -> P
NK -> B
CV -> P
BC -> S
NH -> K
FN -> P
SH -> F
FK -> P
CS -> O
VV -> H
OC -> F
CC -> N
HK -> N
FS -> P
VF -> B
SS -> V
PV -> V
BF -> V
OV -> C
HO -> F
NC -> F
BN -> F
HC -> N
KO -> P
KH -> F
BV -> S
SK -> F
SC -> F
VN -> V
VB -> V
BH -> O
CP -> K
PK -> K
PB -> K
FV -> S
HN -> K
PH -> B
VK -> B
PC -> H
BO -> H
SP -> V
NS -> B
OH -> N
KC -> H
HV -> F
HF -> B
HP -> S
CB -> P
PN -> S
BK -> K
PF -> N
SO -> P
CF -> B
VO -> C
OO -> K
FC -> F
NV -> F
OK -> K
NN -> O
NO -> O
BP -> O
KB -> O
KF -> O

129
16-packet-decoder/code.scm Normal file
View File

@@ -0,0 +1,129 @@
(import (srfi :43)) ; vector extensions
(define (>> x n) (bitwise-arithmetic-shift-right x n))
(define (<< x n) (bitwise-arithmetic-shift-left x n))
(define (& x n) (bitwise-and x n))
; str is a string of hexadecimal numbers
; bit-reader returns a closure that iterators over the bits
; defined by the hex numbers over the requested amount.
; if 'position is sent to closure instead of a number,
; the current bit position in the stream is returned.
(define (bit-reader str)
(let [(index -1)
(len (string-length str))
(available 0)
(nibble #f)]
(lambda (want)
(case want
['position (+ (* index 4) (- 4 available))]
[else
(let reader [(want want)]
(when (= available 0)
(set! index (+ index 1))
(set! nibble
(if (< index len)
(string->number (substring str index (+ index 1)) 16)
#f))
(set! available 4))
(if nibble
(let* [(consume (min want available))
(offset (- 4 consume))
(mask (- (<< 1 consume) 1))
(result (>> (& nibble (<< mask offset)) offset))]
(set! nibble (<< nibble consume))
(set! available (- available consume))
(if (< consume want)
(let [(left (- want consume))]
(+ (<< result left) (reader left)))
result))
#f))]))))
; given a bit-reader, this decodes a variable width integer
; where each packet is 5 bits and a leading bit of 1 tells
; there is one more packet to read
(define (decode-integer br)
(let loop [(value 0)]
(let* [(more? (= (br 1) 1))
(bits (br 4))
(value (+ (* value 16) bits))]
(if more?
(loop value)
value))))
; this decodes each packet in the given hex string
; calling visitor for each with the version type and value of each packet
; for a packet of type 4, value is the literal value of the packet
; for other packets, value is the number of arguments to the opcode.
(define (decode str visitor)
(let [(br (bit-reader str))]
(let loop ()
(let* [(version (br 3))
(type (br 3))]
(if (= type 4)
(visitor version type (decode-integer br))
(let [(length-type (br 1))]
(if (= length-type 0)
(let* [(to-decode (br 15))
(start (br 'position))]
(let sub [(count 1)]
(loop)
(if (< (- (br 'position) start) to-decode)
(sub (+ count 1))
(visitor version type count))))
(let [(count (br 11))]
(let sub [(n count)]
(loop)
(if (> n 1)
(sub (- n 1))
(visitor version type count)))))))))))
; returns the sum of all version numbers of all the packets in the given encoded BITS stream
(define (sum-versions str)
(let [(sum-version 0)]
(decode str (lambda (version type value)
(set! sum-version (+ version sum-version))))
sum-version))
; given a stack, pops "value" as many elements
; and returns a stack with a new head with
; op concatenated with the popped values.
; the rest of the original stack follows
(define (do-op op stack value)
(let rec [(stack stack) (got 0) (output '())]
(if (= got value)
(cons (cons op output) stack)
(let [(v (car stack))]
(rec (cdr stack) (+ got 1) (cons v output))))))
; we can't use regular scheme operators for the evaluation of the BITS stream
; because they return #t/#f instead of 1 0
; so these 3 are helpers to solve the situation
(define (greater-than a b) (if (> a b) 1 0))
(define (less-than a b) (if (< a b) 1 0))
(define (equal-to a b) (if (= a b) 1 0))
; decodes the operation given by the BITS encoded stream and evaluates it
(define (evaluate str)
(let [(stack '())]
(decode str
(lambda (version type value)
(set! stack
(case type
[0 (do-op '+ stack value)]
[1 (do-op '* stack value)]
[2 (do-op 'min stack value)]
[3 (do-op 'max stack value)]
[4 (cons value stack)]
[5 (do-op 'greater-than stack value)]
[6 (do-op 'less-than stack value)]
[7 (do-op 'equal-to stack value)]))))
(eval (car stack))))
(call-with-input-file
"input"
(lambda (file)
(let [(data (get-line file))]
(printf "part 1:~% Sum of version numbers ~a~%" (sum-versions data))
(printf "part 2:~% Transmission value: ~a~%" (evaluate data)))))

1
16-packet-decoder/input Normal file
View File

@@ -0,0 +1 @@
220D4B80491FE6FBDCDA61F23F1D9B763004A7C128012F9DA88CE27B000B30F4804D49CD515380352100763DC5E8EC000844338B10B667A1E60094B7BE8D600ACE774DF39DD364979F67A9AC0D1802B2A41401354F6BF1DC0627B15EC5CCC01694F5BABFC00964E93C95CF080263F0046741A740A76B704300824926693274BE7CC880267D00464852484A5F74520005D65A1EAD2334A700BA4EA41256E4BBBD8DC0999FC3A97286C20164B4FF14A93FD2947494E683E752E49B2737DF7C4080181973496509A5B9A8D37B7C300434016920D9EAEF16AEC0A4AB7DF5B1C01C933B9AAF19E1818027A00A80021F1FA0E43400043E174638572B984B066401D3E802735A4A9ECE371789685AB3E0E800725333EFFBB4B8D131A9F39ED413A1720058F339EE32052D48EC4E5EC3A6006CC2B4BE6FF3F40017A0E4D522226009CA676A7600980021F1921446700042A23C368B713CC015E007324A38DF30BB30533D001200F3E7AC33A00A4F73149558E7B98A4AACC402660803D1EA1045C1006E2CC668EC200F4568A5104802B7D004A53819327531FE607E118803B260F371D02CAEA3486050004EE3006A1E463858600F46D8531E08010987B1BE251002013445345C600B4F67617400D14F61867B39AA38018F8C05E430163C6004980126005B801CC0417080106005000CB4002D7A801AA0062007BC0019608018A004A002B880057CEF5604016827238DFDCC8048B9AF135802400087C32893120401C8D90463E280513D62991EE5CA543A6B75892CB639D503004F00353100662FC498AA00084C6485B1D25044C0139975D004A5EB5E52AC7233294006867F9EE6BA2115E47D7867458401424E354B36CDAFCAB34CBC2008BF2F2BA5CC646E57D4C62E41279E7F37961ACC015B005A5EFF884CBDFF10F9BFF438C014A007D67AE0529DED3901D9CD50B5C0108B13BAFD6070

147
18-snailfish/code.scm Normal file
View File

@@ -0,0 +1,147 @@
(import (srfi :13)) ; string extensions
; returns a new string where all occurrences of `find` have been replaced by `replace`
(define (string-substitute str find replace)
(let [(len-find (string-length find))]
(let loop [(str str)]
(let [(index (string-contains str find))]
(if index
(loop (string-replace str replace index (+ index len-find)))
str)))))
; turns a snailfish number into a regular scheme list
(define (snailfish-number->list str)
; this feels like cheating x)
(get-datum
(open-string-input-port
(string-substitute str "," " "))))
; loads all the snailfish numbers from the given text port, one by line
(define (load-numbers port)
(let loop [(numbers '())]
(if (eof-object? (peek-char port))
(reverse numbers)
(loop (cons (snailfish-number->list (get-line port)) numbers)))))
; updates literal from snailfish number at given index
(define (update-literal! number index updater)
(let [(i 0)]
(let rec [(p number)]
(if (null? p)
i
(if (number? (car p))
(if (= i index)
(begin
(set-car! p (updater (car p)))
(set! i #f)) ; kill recursion
(begin
(set! i (+ i 1))
(rec (cdr p))))
(begin
(rec (car p))
(when i
(rec (cdr p)))))))))
; change literal at given index to be given value
(define (set-literal! number index value)
(update-literal! number index (lambda (v) value)))
; increment literal at given index by given value
(define (add-literal! number index value)
(update-literal! number index (lambda (v) (+ v value))))
; change the car of the cdr of l
(define (set-cadr! l v)
(set-car! (cdr l) v))
; explode snailfish number if possible
; returns #t if any number has exploded
(define (explode! input)
(let [(index 0)]
(let look [(parent '()) (set-parent! #f) (depth 0) (pair input)]
(if (not (number? pair))
(if (>= depth 4)
; explode
(let [(left (car pair))
(right (cadr pair))]
(add-literal! input (- index 1) left)
(add-literal! input (+ index 2) right)
(set-parent! parent 0)
(set! index #f)) ; kill recursion
(begin
(look pair set-car! (+ depth 1) (car pair))
(when index
(look pair set-cadr! (+ depth 1) (cadr pair)))))
; pair is actually a number
(set! index (+ index 1))))
(not index)))
; split snailfish number if possible
; returns #t if any number has been split
(define (split! input)
(let [(index 0)]
(let look [(pair input)]
(if (number? pair)
(if (>= pair 10)
(begin
; split
(set-literal! input index (list (div pair 2) (- pair (div pair 2))))
(set! index #f) ; kill recursion
(reduce! input))
(set! index (+ index 1)))
(begin
(look (car pair))
(when index ; check recursion hasn't been killed
(look (cadr pair))))))
(not index)))
; reduce snailfish number according to the rules of the problem
(define (reduce! input)
(if (explode! input)
(reduce! input)
(if (split! input)
(reduce! input)
input)))
; adds two snailfish numbers
; input will be corrupted!
(define (add! a b)
(reduce! (list a b)))
; compute magnitude of snailfish number
(define (magnitude n)
(if (number? n)
n
(+ (* 3 (magnitude (car n)))
(* 2 (magnitude (cadr n))))))
; deep copy given list
(define (duplicate lst)
(if (pair? lst)
(cons (duplicate (car lst)) (duplicate (cdr lst)))
lst))
; finds the largest magnitude obtained by summing any two numbers from the given list
(define (largest-magnitude numbers)
(let [(largest 0)]
(for-each
(lambda (n1)
(for-each
(lambda (n2)
(let* [(sum (add! (duplicate n1) (duplicate n2)))
(mag (magnitude sum))]
(when (> mag largest)
(set! largest mag))))
(remove n1 numbers)))
numbers)
largest))
(call-with-input-file
"input"
(lambda (file)
(let [(numbers (load-numbers file))]
(let [(numbers (duplicate numbers))]
(printf "part 1:~% Magnitude of final sum: ~a~%"
(magnitude (fold-left add! (car numbers) (cdr numbers)))))
(printf "part 2:~% Largest magnitude: ~a~%"
(largest-magnitude numbers)))))

100
18-snailfish/input Normal file
View File

@@ -0,0 +1,100 @@
[[[[9,5],[9,4]],[[6,5],[7,0]]],4]
[[[5,2],[[7,2],1]],[[[7,5],[0,8]],[[6,9],[7,3]]]]
[[[9,7],[0,1]],9]
[1,[[7,3],[[3,7],[3,2]]]]
[[9,[[0,8],7]],[[3,1],[[6,6],[9,0]]]]
[4,[[4,4],[[7,7],1]]]
[[[[6,2],[5,1]],[[3,3],9]],[7,[[5,7],[5,0]]]]
[[[[4,8],[4,9]],[1,[9,3]]],[1,[1,[6,1]]]]
[[[[4,7],[3,4]],[8,3]],[[3,7],[0,[1,8]]]]
[[[6,[4,8]],[4,5]],[4,[1,3]]]
[[[0,7],0],[[6,[1,8]],[9,[7,9]]]]
[[[[4,8],[3,9]],[4,5]],[1,1]]
[[[4,2],[0,[6,7]]],[[[1,8],2],[8,8]]]
[[[[1,1],7],5],[[6,[5,6]],[6,[7,5]]]]
[[[[3,2],5],[[5,3],1]],[[[0,4],[9,6]],9]]
[[6,[7,6]],9]
[[[[4,0],[0,1]],7],1]
[[[[1,3],4],6],[[1,[4,2]],[1,4]]]
[[[[6,9],[4,1]],[[6,3],[0,8]]],[[4,0],[[3,2],[2,9]]]]
[[[3,6],[[2,0],[3,2]]],[2,5]]
[[[[4,3],5],5],[[4,[4,0]],6]]
[[[[4,0],3],[[3,5],8]],[[8,[4,4]],[[9,9],[4,1]]]]
[[[2,7],6],1]
[[[[5,3],[8,4]],[0,0]],4]
[[[0,[8,1]],0],3]
[[[6,5],[8,2]],[[[6,9],[6,1]],[9,9]]]
[0,[[4,9],6]]
[[9,[[9,9],4]],[[[4,7],1],2]]
[[8,0],[[[0,7],6],[[6,4],2]]]
[[1,[[2,4],8]],1]
[[[[1,3],4],[[1,3],0]],[[[1,2],3],2]]
[[[[2,1],2],[5,[2,8]]],[2,[[6,0],2]]]
[[[8,[1,0]],[[6,7],[9,6]]],[[2,[9,7]],5]]
[[[3,[2,0]],[[3,2],[0,0]]],[[[4,6],[9,4]],[[7,8],[5,1]]]]
[[3,[[9,9],[7,2]]],[[1,3],[2,[3,2]]]]
[4,[4,[[9,5],6]]]
[[[[5,7],7],[[3,4],0]],[[9,[8,2]],[2,3]]]
[[[[2,1],[5,7]],4],[[[6,3],8],[[1,6],[5,1]]]]
[[[4,4],[[0,9],[7,8]]],[[2,[2,5]],5]]
[1,[5,[[3,7],[8,2]]]]
[[[[9,5],[8,6]],[5,5]],[[[9,2],8],[[9,3],[3,8]]]]
[0,[[9,5],[[3,7],7]]]
[[[8,[0,4]],[[2,9],6]],[[6,[8,0]],4]]
[[0,[3,5]],[[5,[0,1]],[[3,6],7]]]
[[2,[7,1]],[[[5,0],[7,7]],[[2,3],9]]]
[[5,[9,[3,9]]],[[8,[3,7]],[[7,6],[3,0]]]]
[[[4,[2,5]],5],[3,1]]
[[[[4,3],1],[[5,7],6]],[0,[3,1]]]
[[8,9],[[[0,7],5],[6,[5,7]]]]
[[6,8],[[5,8],[[8,2],[6,0]]]]
[[1,[5,6]],5]
[[[6,1],[9,[1,2]]],1]
[[5,[7,[4,8]]],[[4,[2,9]],5]]
[[[2,2],[[7,1],3]],[[[9,7],[4,6]],[1,[0,1]]]]
[[3,[6,[4,5]]],2]
[[[0,2],[[8,1],[0,6]]],[[7,[9,6]],0]]
[[[[1,0],[5,1]],[[0,6],5]],[[[1,8],8],[[0,2],5]]]
[[6,[[3,6],6]],[[[9,7],[6,4]],[[9,5],1]]]
[[[0,[5,6]],[9,0]],[[2,9],9]]
[1,[[4,[9,3]],0]]
[[1,0],[[1,9],[4,8]]]
[[[9,3],[7,0]],[[[5,1],[3,8]],9]]
[[[3,9],[[5,9],2]],[[7,2],1]]
[[1,[[3,0],[7,6]]],[7,[8,1]]]
[0,[6,[[7,1],[1,1]]]]
[[4,[[5,0],[2,1]]],[[[8,8],[8,1]],7]]
[[[[9,3],[4,3]],4],[7,5]]
[[9,[[7,4],[8,3]]],[[[1,9],7],[[1,6],[3,1]]]]
[[6,9],[5,[0,[5,1]]]]
[[[8,7],3],[[4,8],[0,7]]]
[[[[3,1],2],[[1,6],[4,3]]],[0,6]]
[[5,[[5,4],3]],[[8,8],9]]
[[5,[3,[4,5]]],[[2,[6,0]],[6,1]]]
[[[[9,5],3],6],[[8,[1,9]],[[5,2],5]]]
[[[7,5],[[3,6],4]],[6,[[5,1],[0,1]]]]
[[1,[[4,8],[1,3]]],7]
[[4,[[4,0],5]],[[[6,2],7],[[4,8],[4,9]]]]
[[[[2,3],[0,9]],[7,2]],[4,5]]
[[[[7,7],[8,0]],[7,7]],[[[6,6],[3,2]],[4,[4,3]]]]
[[[[8,7],6],[[5,5],0]],[[6,[7,3]],[[4,1],[1,7]]]]
[[[2,[2,2]],[[5,2],1]],[[9,[9,2]],6]]
[[[[1,7],6],[[8,8],5]],[6,[1,[1,7]]]]
[[[[8,6],[3,2]],[[5,2],[2,0]]],[[[8,7],2],[[5,5],2]]]
[[[8,[9,0]],[[9,5],[7,5]]],[[5,1],[[1,1],[4,6]]]]
[5,[9,[[0,2],7]]]
[8,[[0,[4,9]],[[7,4],9]]]
[[[[2,9],5],[[0,6],[6,6]]],[[0,6],[[4,2],[9,9]]]]
[7,[[[4,3],3],[[5,4],[6,0]]]]
[[0,[8,[1,1]]],5]
[[[1,8],[[4,6],[9,7]]],[[[6,6],[2,6]],[4,3]]]
[[0,[[7,5],[9,9]]],[[9,7],[6,2]]]
[[[9,[3,0]],[[1,4],0]],[[1,1],1]]
[[[0,7],[[3,0],8]],[[6,[8,0]],[[4,5],[4,0]]]]
[[[[2,9],[4,2]],[5,[9,3]]],[4,[2,[3,4]]]]
[[[1,[7,3]],[[5,7],0]],[6,[[6,5],2]]]
[4,5]
[[7,9],[6,[[6,5],[1,0]]]]
[[4,[[7,5],8]],[[4,0],[[6,6],[0,4]]]]
[[[9,[7,7]],[[4,2],7]],4]
[[0,[0,3]],5]

10
18-snailfish/simple Normal file
View File

@@ -0,0 +1,10 @@
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

77
20-trench-map/code.scm Normal file
View File

@@ -0,0 +1,77 @@
(load "../common.scm")
; returns a vector of the same length of input string
; where all values are 0 except for when the corresponding
; entry in the string is '#' which will have a 1 instead.
(define (string->bin-vector str)
(vector-unfold
(lambda (i)
(if (char=? (string-ref str i) #\#) 1 0))
(string-length str)))
; returns a binary matrix loaded from the file
; all the lines are expected to be the same lengths
(define (file->matrix file)
(let loop [(data '())]
(if (eof-object? (peek-char file))
(matrix-from-data (reverse-list->vector data))
(loop (cons (string->bin-vector (get-line file)) data)))))
; palette enhancing algorihtm:
; for each pixel in input image
; gather all pixels around it to form a 3x3 matrix
; turn that matrix into a binary number, row by row
; look up new pixel value in input palette
(define (enhance image palette steps)
(let* [(new-width (+ (matrix-width image) 4))
(new-height (+ (matrix-height image) 4))
(new-image (filled-matrix new-width new-height 0))
(top-left (matrix-get image 0 0 0))
; if palette 0 is 1, outside becomes all 1 each odd iteration
(outside (if (= (vector-ref palette 0) 1) (mod steps 2) 0))]
(update-matrix!
new-image
(lambda (x y v)
(vector-ref palette (+ (* (matrix-get image (- x 3) (- y 3) outside) 256)
(* (matrix-get image (- x 2) (- y 3) outside) 128)
(* (matrix-get image (- x 1) (- y 3) outside) 64)
(* (matrix-get image (- x 3) (- y 2) outside) 32)
(* (matrix-get image (- x 2) (- y 2) outside) 16)
(* (matrix-get image (- x 1) (- y 2) outside) 8)
(* (matrix-get image (- x 3) (- y 1) outside) 4)
(* (matrix-get image (- x 2) (- y 1) outside) 2)
(* (matrix-get image (- x 1) (- y 1) outside) 1)))))
(if (<= steps 1)
new-image
(enhance new-image palette (- steps 1)))))
; for debugging purposes
; output a PGM 1bpp image file from a given binary matrix
(define (matrix->image-file matrix filename)
(when (file-exists? filename)
(delete-file filename))
(call-with-output-file
filename
(lambda (file)
(fprintf file "P1~%~a ~a~%" (matrix-width matrix) (matrix-height matrix))
(vector-for-each
(lambda (y line)
(vector-for-each
(lambda (x pixel) (fprintf file "~a " (- 1 pixel))) line)
(fprintf file "~%"))
(matrix-data matrix)))))
(call-with-input-file
"input"
(lambda (file)
(let* [(enhancer (string->bin-vector (get-line file)))
(image (begin
(get-line file) ; skip empty line
(file->matrix file)))]
(let [(enhanced-2 (enhance image enhancer 2))]
(printf "part 1:~% Lit pixels after enhancing twice: ~a~%"
(matrix-sum enhanced-2))
(let [(enhanced-50 (enhance enhanced-2 enhancer 48))]
(printf "part 2:~% Lit pixels after enhancing fifty times: ~a~%"
(matrix-sum enhanced-50)))))))

102
20-trench-map/input Normal file
View File

@@ -0,0 +1,102 @@
#######..#.##.##...##.#.#..###..##....######.#.#..#..######.#.#..#####..##.##...#..##........#.#.#...##..##.#####..####.#####..####.#.##.#.#.#.##...##.##.#....###..#...###.#.##..##....##.##.#####..#...#..#....##..##.......##.##....###...#.##...######.##.#######.#.#.#.##.#.#..##.##...##.#.##.#####.#####.###.#....###..###.##.....###..#.##.########..#.#..####..#.###...##...##....##.#.#####..#...##.#..###...##......#.....#.##....##.###..#####..##.###....#..##..##.##.#######.#.##.##.#.####..###.###..#.####..##..
.##..#.#....#..#.###..#..#..###..#.#.#.##.#..##.##...#.##.#.#.###.#.##...#.#####..#.#...#..#.#.###.#
.#..#####....##....#....#......###.##.#.#.#.##...##...##.##.##.###..#.####.######.#.##.#.....###..#.
###..#.##.##.##.....##..#...#..###...#####.##.#...#.#.#.....#.#..##...##..##.#..##.#.#######.....##.
.#.#.####..#.##..####.#.##.##.#.#..#####.####.#.###.####.....#.###.#.####..############...#.##...##.
###.#..#..######.#.###.....##.#...##.....#..#..##...#.#..##.....#.#..###............##.#...#..####..
#..###....####...###.......###.##.###.#.##.##.#.#.##..#...#.##..####..###.....#.#.#######.###...####
#####....#.#.##.#...#...#.#.#..#.#.#......####..####.##.###..####..###...#..#..##.##.#.#####.###.#.#
...#.####.##...#..####..#.#.##........#...##.......##.#.#.###.#..#..#####...#....#.##..#.#.###.#####
.#..#....##..###.#...#...#..####..#...####.#.#.#..####.#.###...#...#..#...####.#.######.##......###.
.####..##.##..###..####..##.###..#.#........##..#...##....##..#..##.#..#..##.####.##.#...###...##..#
#..###...#.#.##..#..#.....#.#........##.#.#..###..#..#.#.#.##.#######.#.##...#.#.#..##..#....#....#.
..#######.#...##...###..#....###...#.#.#...###..#..#####.###....#.#.#.#..#..#.#.#...#.#..##.##..###.
#.##.....##.....##.#..#...###.##.###..#.#.#.##..#.#.##......#.##..##..#.#.#.#.#..##.#.##..###..##.#.
...##..#..#....#.#.#..##..##.##...####..##.##..#...#.....##.....##.#.###.##.###.###......#.#...###..
.##.#.#...#.##..##.###.#.#..#####.#.#....###.#.##.....##.###..####.#.....#..#..####.##.######.#..#.#
##.#.#...#....###..##..###.#.##.#..##..#.#..#.##.####...###....#...#.#.##...####..###...#..#..###.#.
#...####.#...##.#.##...##.##.#..##.##.#.#...#.#.##.#.###.##.#.#...###...#..#.#..###.#.#..##..#..#.#.
#.##.#.#.#..###.###.##..#...#####..###.##..#.##.##..####...###.#..##..##.#....###.#.####.###.#.##..#
.#.#.###.##....##...#....#.#.##..#.....###.#.#.#..##.##.##....#.####.##.#.#....#.####.##..#..#.##...
####.#......#.####.#...#.####...#.#.#.#..##..#.#.#.###.######.....#.#.#..##.###..##....###.#...##.#.
.#..##.#..###.#.##.#.##..###.#..##...#...##.#.#...#.##..##.##.##.....##.##..##..###.##....#.##..#...
#.#.###..####..#.#####...#.##....####..#.#.##.###....##.#######..#.#..#.##..##...#.....##.#.#.##.#.#
...#..##.....#.###.#...#.##.#......##..##.....##..###.##...####.#...###..###.#.###..#.########.##.#.
..##.#...#..####....###.####.#..#..#.....#.###.#..#..#..#.####.....##..#.#..#####.##...#.#..#.#.#...
.####...#.#...#..##...#......#.###..##...#.#.#.###.#...####.#####..#..#.########..###.##...##...#.##
##.#.#..###.####.#....######.#####.#.##.#.##..#..##....##.###.##..#.##....###..##.#.....###.####.#..
...#.#.#.....####..######.##...#..#..##.#######..#.....###.###.......##.###...#..##..#.###.###......
..#......#.#.......###.#######..####.#........##.#..##...###....#...###.....#.#.#..###..#.#.#......#
...#.##.#.###.#....#..#.###.###.##......######.#.###.##.##.......#..#.....##..##...#....#.######.#..
##.#...#..#.##.#..#.#.....#####.......#.##..#..###........#.##.###..#..##.#......##..#.#.##....##.##
.##.#.#####.###...###.#..#..##..#.##..##.#..#.####..#######..############..#...#..###....####..#.#.#
#.#..#...##.#.###..#...###.##..###.#.#.#.#....#.#.##.#..#.#...#.#..###.......###.#..#..#.....####.##
..#..###...##..#.#.#...##..#...##.##.#..###.#.##.####....#...#..####.#.#.###..#....#...#.#####..##.#
#..##...###...#####.###...####.#..#...##.#..#...####.#.##..####..###.#..###.#..#.##.#.#....#...#...#
...#.#.#####.###..##.#..##.#.....#..#..###.#.####..######..#.....####.#...##.#####.###.###.#..##.#..
.....#..#.#...###.#.#.###.###...###...##.....##.##...##..#.#..#...#...#######.......#.#.#######..#.#
######...##.#...#.#.####..#......##..#.#.##.#..#####.#..#....#..#.....##.####.####.#.##.##..#..#....
#.#.#.#####....###.##.##...##.#.#.....###...#.#....#.####.#.#......#.#######...#.###....####.#......
#...####.#......#.....##...#####.##..##..#.##..##.#.#..#.#..#.#.##.....#.##.#.##.###.....#.####.#.#.
..###.#..##.####..##..#...##.#....#.....#.#####..#.#.####.###.#.####.#..###....#.#.#...#.#.....#.##.
.##.#....#.....#......#.#.......####....##.###.#...#.#.#..#...##.#.##.##.#...###.#......#.....#.####
##.#..##..###...#.##..#####...#.##......#..#..##.#.###....###..#..##...#####.#######...#.#..#...#.##
...#.#.#.#.#....#.####....#####..###...#.#..#.#....#.#.#......#.##.....####.....#####....#.#.#...###
############...###.##.##.....##..####....###.##..#####...#.###.#######.##.####.#....##.#.###..###.#.
.##.#.##.###...#.######.##..##.#.#####.######...####..###..######.###.#...####..####.#.##.###.#.....
#..#..#.#........#.##.#.........#####..#......######.###...#.#.....##.##..#####.#..##.#..##..#######
..##....#.#.####..#....#....#...##.####.#......#......####..##.....#..#..#...#..#.###.##....###..#.#
#...##.#..##...#....####...###..#..###..#.....##.#.###.#..##.#.#.#.#......#.....#..####.##.........#
##.#.##.##.#.##...#.##.#.#.#.#..#....###..#..#.......##..#.#.##...#.####...##..###.##...#...#.#...##
#.##.#.#...#....#.##..##.#...##..#.....###.##..####.##...#####....#.###.##..#..###..#..##.#....##..#
#.#.#..##.#.#....##..###.#..####....###.#..#.#.####.#.##.......#..###.#......#.##.##.###.#######..##
.#.####.#.....#.#####.#.##.####.....#.....##.#.##.......#..#.#..#..#....##....#...##.#######.#.####.
#####.###.#..##.#..####.##..##........###.######...#..###....#.#.....#..####.##..###..####.#.#....##
.##...###...###.###.#.##..#.##.##..#.#.##.##..#.###.##.........#.##.####.###...##..##.#.#...#.....#.
.#..#.#.....#.#.##..#..#######...#..####.#...#.###.###.###.#.##..#...##.#.######.#.#.##.##..#..###..
#####.#....#.#.....#...##.#.#.#.##...#.#.###.#.....#....#......#.#.####.##.###.#..#..#.#.##...#....#
##.....#........#.#.#####.#..####...##...##.#.#.###.....###..#..#.#.#.##.###.#.###.##..#.#.###....##
#..#.#.#.#.#......#...#..##.####....#..#....#..#.#...#.#.#.##..##.#.####.#..###..##..##.####.#..#..#
#..#.####.#..###.##.#....###..#####..#####..##.########..#..###..###..#.###.#.#..#..###..#..#.#..###
.#.######.##..#..#.###..######...#.###.....#.##..#...####......#.##.#.#.##.#.#...#..##..#.#####.##..
..#..#.#...#.....#.##.##.#####.###..#.##..#####.#.#..##.....#..####....#...##.#...#.####.#####...###
#.##...#.##.###.#..##..#.########.#.#...##.#####..##..#####.#####.######.######....#...####..#.#.#..
.##.##..##.#...#.......#.###.####.#...##....####..#..###...#####.#...#.##.####.##.#.#.##.#..###.#..#
.#..#.#.#..#.###....###..##.....#....#....####.#.##.##.....#..#..#..#.###....##......#.#..##...#..#.
##...###.###......#.#..#.#.#######.##.#...#.##.#####....##...#..#.....###....##.#.#.###..#....#.#...
.##.##.#..#.###.###.#..#.######....#.##..##.#..#...#.#.###...#.#####...##.#.##..##..##..####..#.#..#
.##.....#....###..##.##.#.....#..######....##.###..#..#...##..#....###.####..#####...####..###.#...#
####.###...#...###...#####.#..#######.#####..#...#.##...###....#.#...#..#####.#..#..##.#.....#####.#
#.....#...######.#.#.#.#.#.###.####.#..#.....#..##.##...#.#..##..#..#.....#....######.##.#.##..#....
#####..#..##...#....#.......#.#....##.#.#..#.#.##...##.#....####..#...#.##..#.#.#.#...#.###...###.#.
.#.#.####....#.##.###.##..###..###.##..###..##...#.##.#.####..#....#..#....#...##...##..###..##....#
.##....###.##.#...#........#...###...#..###.##.####.#.##.#.##.###..#.##..#..#...##...##...##.#..#.#.
#..##.#..#######.##.#.#.#.####.#..........#.#..##..###.##.#.#.....#....##.#####..##.###..#.#....#.##
.##.#.#.###...#...##..#####..#.###..########..#...##...###..#..###..##.....####.###..###..##.##.##.#
#..##.##.#...##..##...##..##.###....#.#...######.####.##.###.#.#..##.#.###..##.###...#.#####..###.##
..##........##..##..#.#...#.#..##.##..#..#...#.#.#..####..#...####.##...##.#....#.##.###...#.#.##...
..#.#..#.#.##.##.##.##.#....##.#######.#....##.##.......##.##..#.##..#....#.####.#.#.....#.###.###..
#####...#.#..#.##..####...#..##.#####....#.###.##..########..###.######.#..#.#..##.#..#..#.######..#
..#...#.#....#.....#######..#.##.#.#..##.......#..##...#.##..###.#.#..##.#....###.#........#......##
#####.#..#.#.#..####...#.#......##.##.##.#...##.##...#.####...####......##..##..#.##.#####.###......
#.#..#.#..##.###.##.##..#.#..#.###...#...###.##.#.#....#..##..##...#.###.#......#.#.###..#.####...#.
.##..#.##.#.#.##########.##.##....#.####.#.#...###.#...#.#...##.##..####.....###.##.....#.#..#.#.###
###.##.#...#.##.##.#.##.##.#.#.#.###...##..#....#.##.#..####..#..#.#.#.#......##........#..##..#..##
####...#.##.##.###.#.##.#.....###..##.#..##.##..#.##..#.##.#.##..#......#.#..##.##...#..#.#..#.#.#..
###..#.##.##.#.##.#.#..#....#..##.##...#.......#.#..#.###.#..##.#.########..##.#..##.###...#.#.##.##
...##.#..#####..###.#.#....####.##.##....#.#.##..#..##..#...#....###.##.....#.#...####.#####....#.#.
#.####....#...#####..#..#.##..#..###..#.#....#..#.#######....#....#.#...#.####......#.##...#...##.##
##.#.#.#...#....#.....###.####.####.##.......####.####..#...#...#.#..#.#.##....#.#...#.########....#
##..#...#.##.##..#.#.##...###.##..#..#...#.#.#.#...#.#..#.#..#...#.#...#.#.##.###.....#.##.####.#...
.###..#....#.....#.#.#..#..#######...#..#.###..##.#.#####.#..###....#.#..#.##..#.#.#......#..###..##
##..#.####.##..###.##..#.##....##.##....######..#.##..###.##.#.##..####..#.###.###.#.####..#..######
.#.##.##...#....#.#.#..##.##.#####.##.#.###.##.....#.##.####...#..##...#.####.#.#.....#...###...###.
...#...###.......###...#.#..#....#...##.#.#####.###.#..#.##.#.##.##...#...##########.#.#.#.###.#.#..
###...#.......#.#.#.###...#...####.#.##.##..#..###.####.#....##.#.##..#.####.########.######.##...##
.#...##..#...#..##.##.#....#.##..###.#..###......###..###.....#.#.#..#.####.#...###....####...##..#.
#.##.###.#####.####.#.#.........#...#....###...#.#..#.#...###...##..#...#..#.#.#..#####..#.####.##.#
...##.###.###..###.#.####.##.#....#.....#.#.###..#.#.....#.#.##..###.#.##.#.#.##..###...#.##..##..#.
##...#.#.##..####..#.##........#..#.####.####.#..#..###.#.#####..#....#...##.#.###......#.#####.#.#.
#...##..#.###..#.#.#...###..#.#.#....#....#..##.###.#.#....#....#.#.#.##...#.#..##....#.####..##.###
#.###.#....#.###..####..#..###.##......#.#...##.##.##.#..#...##...#.####...##.#.#...###..##..##.####

137
21-dirac-dice/code.scm Normal file
View File

@@ -0,0 +1,137 @@
(load "../common.scm")
(define (read-starting-positions filename)
(call-with-input-file
filename
(lambda (file)
(let* [(player-1 (begin
(get-string-n file (string-length "Player 1 starting position: "))
(get-number file)))
(player-2 (begin
(get-string-n file (string-length "Player 2 starting position: "))
(get-number file)))]
(values player-1 player-2)))))
; returns a lambda that yields increasing numbers from 1 to 100, then repeating back from 1
(define (deterministic-dice)
(let [(value -1)]
(lambda ()
(set! value (mod (+ value 1) 100))
(+ value 1))))
; play a game of dirac with a magic deterministic dice that always yields increasing values
; returned value is multiplication of dice roll count and final score of opponent
(define (play-deterministic position-1 position-2 win-score)
(let [(dice (deterministic-dice))]
; we subtract one from position so that we can just use "mod 10" to wrap around
(let repeat [(player-1 (cons (- position-1 1) 0)) ; each player pair is (position . score)
(player-2 (cons (- position-2 1) 0))
(rolls 3)]
(let* [(new-position (mod (+ (car player-1) (dice) (dice) (dice)) 10))
; add 1 to account for off by 1 positions
(new-score (+ new-position (cdr player-1) 1))]
(if (>= new-score win-score)
(* (cdr player-2) rolls)
; swap players in next iteration
(repeat player-2 (cons new-position new-score) (+ 3 rolls)))))))
; state of an universe playing dirac, two players pawn position with their scores
(define-record-type state
(fields
; positions are 0..9 instead of input's 1..10
; so we can just mod 10 while incrementing position
(mutable position-1)
(mutable score-1)
(mutable position-2)
(mutable score-2)))
(define (state-copy state)
(make-state
(state-position-1 state)
(state-score-1 state)
(state-position-2 state)
(state-score-2 state)))
(define (state-equal? l r)
(and (equal? (state-position-1 l) (state-position-1 r))
(equal? (state-position-2 l) (state-position-2 r))
(equal? (state-score-1 l) (state-score-1 r))
(equal? (state-score-2 l) (state-score-2 r))))
(define (state-hash state)
(+ (state-position-1 state)
(* 6700417 (+ (state-position-2 state)
(* 6700417 (+ (state-score-1 state)
(* 6700417 (state-score-2 state))))))))
(define (make-state-hashtable)
(make-hashtable state-hash state-equal?))
(define (hashtable-for-each proc ht)
(let-values [((k v) (hashtable-entries ht))]
(vector-for-each (lambda (i k v) (proc k v)) k v)))
(define (hashtable-empty? ht)
(= (hashtable-size ht) 0))
; sum and amount of permutations of 3 3-sided dice
(define dirac-dice
'((3 . 1) ; (1 1 1)
(4 . 3) ; (1 1 2) (1 2 1) (2 1 1)
(5 . 6) ; (1 1 3) (1 2 2) (1 3 1) (2 1 2) (2 2 1) (3 1 1)
(6 . 7) ; (1 2 3) (1 3 2) (2 1 3) (2 2 2) (2 3 1) (3 1 2) (3 2 1)
(7 . 6) ; (1 3 3) (2 2 3) (2 3 2) (3 1 3) (3 2 2) (3 3 1)
(8 . 3) ; (2 3 3) (3 2 3) (3 3 2)
(9 . 1))) ; (3 3 3)
; roll dice creating as many necessary universes in the process
; returns amount of wins obtained with this call and new states
(define (dirac-roll win-score states player)
(let-values [((get-position get-score set-position! set-score!)
(if (= player 1)
(values state-position-1 state-score-1
state-position-1-set! state-score-1-set!)
(values state-position-2 state-score-2
state-position-2-set! state-score-2-set!)))]
(let [(additional-wins 0)
(new-states (make-state-hashtable))]
(for-each
(lambda (outcome)
(let [(sum (car outcome))
(permutations (cdr outcome))]
(hashtable-for-each
(lambda (state amount)
(let* [(new-position (mod (+ (get-position state) sum) 10))
(new-score (+ (get-score state) new-position 1))
(universes (* amount permutations))]
(if (>= new-score win-score)
(set! additional-wins (+ additional-wins universes))
(let [(new-state (state-copy state))]
(set-position! new-state new-position)
(set-score! new-state new-score)
(hashtable-set! new-states new-state
(+ universes (hashtable-ref new-states new-state 0)))))))
states)))
dirac-dice)
(values additional-wins new-states))))
; play dirac dice starting from given positions up until the given score
; returned value is maximum number of wins for any of the two players
(define (play-dirac position-1 position-2 win-score)
(let [(init-state (make-state-hashtable))]
(hashtable-set! init-state (make-state (- position-1 1) 0 (- position-2 1) 0) 1)
; "states" tracks the amount of universes currently using a given state
(let repeat [(states init-state) (player-1-wins 0) (player-2-wins 0) (player 1)]
(let-values [((additional-wins new-states)
(dirac-roll win-score states player))]
(set! player-1-wins (+ player-1-wins additional-wins))
(if (hashtable-empty? new-states) ; have all universes finished playing?
(max player-1-wins player-2-wins)
; swap players in next iteration
(repeat new-states player-2-wins player-1-wins (- 1 player)))))))
(let-values [((position-1 position-2) (read-starting-positions "input"))]
(printf "part 1:~% Rolls * Opponent Score = ~a~%"
(play-deterministic position-1 position-2 1000))
(printf "part 2:~% Max Winning Universes: ~a~%"
(play-dirac position-1 position-2 21)))

2
21-dirac-dice/input Normal file
View File

@@ -0,0 +1,2 @@
Player 1 starting position: 10
Player 2 starting position: 6

View File

@@ -0,0 +1,74 @@
(load "../common.scm")
(define (read-cuboid-operation file)
(let* [(on/off (if (symbol=? (get-datum file) 'on) 1 0))
(x-start (begin (get-string-n file 3) (get-number file))) ; skipping " x="
(x-end (begin (get-string-n file 1) (get-number file))) ; skipping dot
(y-start (begin (get-string-n file 2) (get-number file))) ; skipping "y="
(y-end (begin (get-string-n file 1) (get-number file))) ; skipping dot
(z-start (begin (get-string-n file 2) (get-number file))) ; skipping "z="
(z-end (begin (get-string-n file 1) (get-number file)))] ; skipping dot
(list on/off x-start x-end y-start y-end z-start z-end)))
(define (load-cuboid-ops filename)
(call-with-input-file
filename
(lambda (file)
(let loop [(ops '())]
(if (eof-object? (peek-char file))
(reverse ops)
(loop (cons (read-cuboid-operation file) ops)))))))
(define (make-3d-vector x-size y-size z-size init)
(let x-loop [(x 0) (planes '())]
(if (= x x-size)
(reverse-list->vector planes)
(x-loop (+ x 1)
(cons
(let y-loop [(y 0) (lines '())]
(if (= y y-size)
(reverse-list->vector lines)
(y-loop (+ y 1) (cons (make-vector z-size init) lines))))
planes)))))
; set 3D volume to given value
; end ranges are inclusive!
(define (vector-3d-set! 3d-vector value x-start x-end y-start y-end z-start z-end)
(let [(width (- (vector-length (vector-ref (vector-ref 3d-vector 0) 0)) 1))
(height (- (vector-length (vector-ref 3d-vector 0)) 1))
(depth (- (vector-length 3d-vector) 1))]
(let x-loop [(x (max x-start 0))]
(unless (> x (min x-end width))
(let y-loop [(y (max y-start 0))]
(unless (> y (min y-end height))
(let z-loop [(z (max z-start 0))]
(unless (> z (min z-end depth))
(vector-set! (vector-ref (vector-ref 3d-vector x) y) z value)
(z-loop (+ z 1))))
(y-loop (+ y 1))))
(x-loop (+ x 1))))))
(define (vector-3d-sum 3d-vector)
(vector-fold
(lambda (i a v)
(+ a (vector-fold
(lambda (i a v)
(+ a (vector-fold
(lambda (i a v) (+ a v))
0 v)))
0 v)))
0 3d-vector))
(let [(operations (load-cuboid-ops "input"))
(grid (make-3d-vector 101 101 101 0))]
(let operate [(op operations)]
(unless (null? op)
(apply vector-3d-set! grid
(cons (caar op)
; offset x/y/z ranges from -50..50 to 0..101 to fit in 3d-vector
(map (lambda (x) (+ x 50)) (cdar op))))
(operate (cdr op))))
(printf "part 1:~% Cubes powered after initialization procedure: ~a~%"
(vector-3d-sum grid)))
;; do step 2 using a sparse voxel octree ?

420
22-reactor-reboot/input Normal file
View File

@@ -0,0 +1,420 @@
on x=-40..11,y=-14..32,z=-31..22
on x=-12..41,y=-30..15,z=-32..21
on x=-9..44,y=-33..17,z=-4..45
on x=-18..35,y=-40..10,z=-38..9
on x=-24..30,y=-11..39,z=-13..37
on x=-41..9,y=-36..10,z=-23..26
on x=-43..9,y=-29..18,z=-19..29
on x=-10..38,y=-36..12,z=-38..11
on x=-49..-3,y=-34..10,z=-7..37
on x=-48..2,y=-2..45,z=-6..41
off x=21..38,y=10..24,z=-28..-12
on x=-16..28,y=-29..24,z=-14..31
off x=-13..0,y=-39..-23,z=-14..-4
on x=-49..-4,y=-35..18,z=-42..4
off x=-40..-25,y=-4..7,z=-28..-9
on x=-6..40,y=-26..24,z=-31..15
off x=-14..-1,y=-4..15,z=-28..-16
on x=-14..37,y=-11..41,z=-44..5
off x=10..23,y=7..16,z=0..12
on x=-18..29,y=-21..26,z=-13..39
on x=52704..79839,y=37868..51010,z=-3392..33510
on x=38091..58614,y=46281..66998,z=28429..43580
on x=-9985..-2240,y=-86835..-71154,z=-52538..-20590
on x=19142..39549,y=-66453..-43948,z=-50566..-30845
on x=-53282..-40034,y=4170..39400,z=59398..76192
on x=32470..54803,y=-47063..-26918,z=-73686..-60192
on x=365..23050,y=-4035..24108,z=67622..79241
on x=-37738..-17266,y=33985..60729,z=33884..49177
on x=-83482..-60595,y=-2931..7326,z=-48357..-28454
on x=-83100..-69364,y=-26771..-8585,z=-45292..-25066
on x=3083..37031,y=-31937..-20818,z=-75053..-58054
on x=-41592..-38250,y=57697..83195,z=19481..22330
on x=61560..74632,y=-48382..-26457,z=-24553..-11151
on x=56483..66008,y=-65722..-47950,z=-16632..3501
on x=35470..60787,y=19347..35344,z=-60634..-49056
on x=-49335..-35993,y=-42627..-34877,z=-72532..-43412
on x=27246..52219,y=50626..61782,z=27719..51216
on x=-85500..-47546,y=19929..45970,z=-27611..-8367
on x=71585..85828,y=-313..13728,z=-23500..-15959
on x=-80663..-64029,y=15111..33015,z=-37149..-22636
on x=51883..74316,y=-43876..-29167,z=-10921..20301
on x=-8903..11201,y=7652..32695,z=58630..86739
on x=-57670..-22158,y=-61857..-50981,z=-46401..-25524
on x=31912..44707,y=64053..73856,z=-17747..4340
on x=-81173..-65386,y=-27630..-16387,z=-6811..14879
on x=40185..69278,y=3762..26275,z=-70046..-55571
on x=-19446..2679,y=-6114..11039,z=-99140..-68449
on x=17709..36452,y=-82309..-52721,z=23100..51328
on x=2043..15305,y=1442..32965,z=-92073..-64481
on x=35558..49216,y=-70325..-66711,z=-21170..3146
on x=10674..31920,y=72256..86964,z=-11608..10702
on x=53369..58001,y=24914..35865,z=27523..58385
on x=24522..43385,y=-34626..624,z=-83512..-55878
on x=23785..44762,y=-12095..10348,z=63376..76916
on x=41138..69895,y=-67681..-49963,z=-9760..4927
on x=-29110..-8604,y=-83207..-64595,z=28982..46575
on x=-9025..27914,y=-13944..18216,z=-90080..-71745
on x=-36304..-13127,y=56941..92240,z=-12741..14434
on x=38582..58139,y=49659..79497,z=12376..24782
on x=61849..78055,y=-24891..-20174,z=12803..25456
on x=-75610..-51867,y=-69260..-39815,z=-19600..761
on x=18480..42656,y=-54526..-41499,z=47513..57130
on x=45816..60222,y=45301..62037,z=-1369..20213
on x=-34769..-3617,y=38726..52174,z=-80544..-56963
on x=-57956..-33180,y=-37129..-12713,z=45122..64539
on x=72557..81573,y=23414..37444,z=1736..23802
on x=5090..9279,y=2838..16266,z=-79692..-69436
on x=-5514..12147,y=-59116..-41174,z=41321..68246
on x=-65118..-44815,y=26318..48837,z=11357..32130
on x=-57663..-43348,y=-50124..-31196,z=-57993..-26678
on x=42037..72166,y=18398..46480,z=-55872..-23677
on x=27866..62028,y=-24997..1347,z=-76775..-63913
on x=-78972..-62598,y=-31207..-10228,z=32094..50548
on x=-33099..-11768,y=-81931..-57524,z=-51235..-25105
on x=-54019..-32105,y=-53032..-32099,z=-56645..-40064
on x=52537..84660,y=-54482..-35322,z=-23971..-14824
on x=-21600..1515,y=55484..93182,z=-36836..-18468
on x=15584..25508,y=-70746..-52630,z=-67974..-44586
on x=60478..71170,y=-52080..-34095,z=-32825..-13219
on x=675..33463,y=-31882..4457,z=73614..78393
on x=-8025..11786,y=-33236..-7056,z=69961..85841
on x=18959..33208,y=14370..28122,z=-88556..-64853
on x=38451..58826,y=61376..82573,z=-5686..16871
on x=-78273..-41338,y=-19295..-3240,z=43019..55216
on x=-70384..-44212,y=20288..55004,z=-43470..-36032
on x=-29018..-8082,y=69454..76436,z=4523..20529
on x=-79693..-64317,y=12904..26572,z=23251..55857
on x=-21693..4220,y=54543..76570,z=-61644..-32458
on x=-84533..-63448,y=27676..60364,z=-15560..788
on x=29423..37719,y=-84580..-53221,z=-46620..-15506
on x=-23430..-19125,y=-70013..-50002,z=-71754..-43166
on x=51955..86000,y=42270..45907,z=-12209..7709
on x=-70149..-42895,y=40525..61461,z=12948..26240
on x=65179..92217,y=1105..16789,z=19330..24791
on x=-31302..-10320,y=-66014..-43091,z=30866..61258
on x=-13652..112,y=1234..25410,z=64006..78619
on x=-64905..-61392,y=-32367..-24114,z=-50048..-39852
on x=-32684..-8708,y=65860..85813,z=-43053..-10935
on x=-19685..-17005,y=-462..14069,z=-82259..-68931
on x=30763..41712,y=11802..33697,z=48021..77340
on x=-32742..-30194,y=-63276..-50051,z=-56206..-47157
on x=-6034..19641,y=47244..81195,z=34950..58900
on x=-77922..-74492,y=-20149..3206,z=-25810..-19993
on x=-57499..-24019,y=-60909..-41623,z=-43908..-19195
on x=-83420..-46643,y=-50366..-38554,z=-34003..-9442
on x=-4674..20615,y=-6548..8579,z=76849..99643
on x=-83441..-57514,y=-14654..2677,z=23403..55461
on x=-13004..17977,y=56129..80501,z=20673..44006
on x=5783..16409,y=49402..79074,z=34635..56108
on x=-14816..-4412,y=-71549..-45114,z=49313..69585
on x=7742..17133,y=-44451..-23194,z=61394..80488
on x=35492..61007,y=27099..49109,z=33765..60791
on x=9907..27615,y=-75325..-54303,z=-51172..-25946
on x=-66237..-53723,y=11724..28147,z=-51276..-44587
on x=-28219..4854,y=25691..56400,z=-81222..-54723
on x=-2753..12385,y=-34766..-19347,z=-75679..-62461
on x=-58397..-50546,y=-41226..-29572,z=-55916..-47964
on x=-40890..-22005,y=-31979..-14263,z=56523..85634
on x=-85097..-66136,y=-25326..-4376,z=16945..39474
on x=-70572..-42157,y=-58941..-40522,z=-787..9228
on x=59496..77542,y=649..29118,z=18157..25701
on x=-16620..8817,y=4470..32017,z=72464..92266
on x=-58954..-30688,y=-49810..-38762,z=31370..54067
on x=-8415..22974,y=-15472..2808,z=-83591..-72277
on x=4465..36422,y=9316..23887,z=-81737..-59694
on x=40401..72041,y=45629..73683,z=-3051..8894
on x=-75865..-38666,y=-74261..-50297,z=-30108..3423
on x=-65746..-40424,y=27346..55956,z=-57038..-46491
on x=-34057..-28732,y=20350..28851,z=58692..76389
on x=58754..75968,y=-50324..-23367,z=-9231..16998
on x=19853..27875,y=-64336..-42100,z=-62570..-42133
on x=-78730..-61939,y=-37002..-3772,z=-14768..-1026
on x=-7513..9951,y=47849..58353,z=-60397..-49547
on x=-15988..4060,y=-32606..-13519,z=63938..90251
on x=-56561..-20153,y=24700..42969,z=-63604..-44549
on x=-47823..-28482,y=29288..49370,z=-76780..-44864
on x=-93003..-61581,y=-3710..9729,z=-43306..-18610
on x=-7591..9224,y=59782..67871,z=42615..65372
on x=44509..62451,y=-4674..16228,z=-67178..-55159
on x=23712..50710,y=-59738..-34178,z=37531..50497
on x=-53691..-34641,y=-14707..4535,z=-68836..-48475
on x=-74764..-50556,y=52172..60861,z=-15558..11376
on x=-2870..4996,y=-83125..-65858,z=-61717..-32912
on x=39799..59151,y=-76027..-45986,z=551..12174
on x=-64523..-31479,y=-73598..-52586,z=11654..32905
on x=-34618..-11833,y=-4547..24985,z=71949..86052
on x=52719..73073,y=-37395..-7648,z=-61951..-31067
on x=62262..80347,y=-58054..-35019,z=438..20726
on x=3826..30796,y=23300..53655,z=55673..68408
on x=-86746..-64924,y=-16179..493,z=15087..30156
on x=-83292..-59778,y=-6830..22288,z=34821..54793
on x=-75976..-60101,y=-37042..-17973,z=28159..46174
on x=-68930..-48647,y=-18017..-7360,z=-76892..-41811
on x=48824..75202,y=45033..61487,z=11020..29026
on x=12598..43235,y=-92130..-69843,z=-21403..-10227
on x=-22635..-6190,y=-91694..-67430,z=-986..17723
on x=7923..39148,y=-52003..-37193,z=-70391..-58456
on x=-53583..-27620,y=-5213..15222,z=53255..72002
on x=-41186..-32110,y=-71247..-55124,z=-48278..-37938
on x=-43468..-36785,y=-65347..-52003,z=16110..35159
on x=55753..72160,y=33837..43804,z=11867..42632
on x=60974..65802,y=-7872..21816,z=38624..59191
on x=-67834..-43169,y=59746..76318,z=-19319..-3837
on x=7716..27739,y=-79156..-42703,z=48237..53567
on x=-43320..-7337,y=-9875..16274,z=-87523..-67003
on x=51992..85555,y=-42756..-22688,z=-37100..-13882
on x=-56004..-31347,y=-42166..-18897,z=-76741..-54304
on x=-25867..6502,y=55524..93420,z=-29026..-9793
on x=-67884..-40383,y=15259..46945,z=-63028..-42461
on x=-85541..-61455,y=-33920..-26474,z=-25824..-4677
on x=-17197..-1776,y=-4434..15207,z=61349..96039
on x=-30791..-1886,y=76838..80524,z=505..24315
on x=-87685..-63438,y=-15555..17450,z=-49253..-23288
on x=57748..61519,y=28610..42516,z=35671..46553
on x=-2015..15944,y=-72411..-54213,z=-49669..-12051
on x=-3194..19653,y=2949..22975,z=-88617..-61219
on x=-35765..-13698,y=40907..61560,z=-72724..-46741
on x=62383..75558,y=-23338..-8827,z=25803..33402
on x=-29777..-9621,y=35529..57255,z=53593..70308
on x=-62964..-36806,y=37032..60809,z=-32432..-24673
on x=-97798..-65820,y=-2046..18226,z=-2911..14055
on x=-67735..-62278,y=6742..30575,z=27185..58234
on x=9161..14538,y=-13634..6118,z=73431..93259
on x=25042..56972,y=-71072..-49999,z=-43164..-8732
on x=33644..53708,y=-65074..-35473,z=27759..37217
on x=-59329..-33189,y=11762..35516,z=53884..74951
on x=54341..71932,y=32470..52791,z=7383..31963
on x=-44191..-35773,y=-69187..-51344,z=18074..30283
on x=-68840..-55582,y=-49625..-28992,z=24447..34108
on x=-65876..-49424,y=-48346..-37217,z=47211..68167
on x=-14855..9371,y=44197..81649,z=40647..49641
on x=-12557..11583,y=56425..76856,z=12910..33763
on x=37057..69765,y=32068..58873,z=21652..56601
on x=-62655..-46250,y=-62511..-36495,z=-10420..1406
on x=-91382..-56907,y=-15204..-820,z=24434..30859
on x=35173..61603,y=52767..59628,z=29560..46766
on x=28989..42910,y=-63464..-46737,z=35329..59451
on x=-33177..-1918,y=57713..78889,z=6674..21509
on x=38131..53449,y=26890..47819,z=-69905..-44532
on x=-1096..28567,y=-31065..-23063,z=70999..85147
on x=5460..17671,y=28358..60430,z=-73500..-52976
on x=-83642..-64530,y=-34198..-3664,z=-31589..-8117
on x=-42162..-18967,y=44879..53373,z=-71006..-37807
on x=31972..68502,y=50644..70694,z=-44051..-24087
on x=46135..62945,y=-14786..-2315,z=-74565..-43578
on x=51216..70714,y=-48792..-29081,z=-62105..-37473
on x=76230..90231,y=-25563..12654,z=8157..11904
on x=-38622..-7948,y=-53310..-46283,z=43185..79826
on x=61191..91573,y=-18044..-14051,z=-13663..4674
on x=-47608..-22619,y=25075..40696,z=58665..67626
on x=10993..19023,y=69185..76251,z=-27342..-18384
on x=-19252..1540,y=-26273..-13064,z=-90423..-72846
on x=-46245..-30340,y=44054..78785,z=-36608..-29062
on x=77101..90813,y=-1918..35613,z=-9148..21020
on x=-17256..-4415,y=10693..16966,z=-77723..-61545
on x=36680..53471,y=-54500..-27258,z=-68571..-45043
on x=36111..65474,y=44239..59980,z=-42193..-15264
on x=37167..40644,y=-67585..-58894,z=4910..19720
on x=-58726..-56160,y=44363..62870,z=15606..43570
on x=37640..55251,y=3043..18953,z=58556..78120
on x=37858..58649,y=39917..45555,z=44634..48416
off x=-46555..-27774,y=-58044..-55447,z=26948..52717
on x=49576..58154,y=-63033..-35935,z=35230..51602
on x=-25982..-8334,y=6908..15037,z=63794..93030
on x=23029..55417,y=3808..25803,z=-67334..-51922
on x=-62115..-42167,y=-38261..-12458,z=38635..53594
on x=919..16394,y=29372..52318,z=-78525..-68484
off x=-28864..-10231,y=36906..63059,z=42489..55686
off x=-9266..-2678,y=-52112..-47170,z=51609..68293
off x=19176..41133,y=-60373..-27044,z=40737..73140
off x=-87143..-56546,y=-39505..-32252,z=10112..40348
off x=54380..64016,y=-11258..1980,z=50846..57214
off x=-30048..-4331,y=41036..60646,z=-75931..-62373
on x=-72858..-56035,y=-25628..-7,z=18211..42527
off x=407..5447,y=-91691..-69729,z=-17777..-4698
off x=8012..34916,y=-80699..-66794,z=15939..30656
on x=-69285..-36964,y=-50161..-36296,z=20703..54547
on x=-50910..-31238,y=29560..47395,z=48971..65934
on x=7319..31549,y=66374..80104,z=-9640..4096
off x=3161..14670,y=43473..64705,z=48039..74095
off x=-4495..12342,y=39701..51757,z=60670..65041
on x=55647..70724,y=-48459..-23208,z=-27649..-6863
off x=-65128..-39894,y=45568..64986,z=27101..41648
off x=-50713..-39240,y=10590..33139,z=-64638..-52204
off x=60576..66821,y=483..6779,z=33262..65343
on x=69502..89323,y=4590..20962,z=4712..21277
on x=-24245..-956,y=-87155..-51921,z=-43436..-18128
off x=-1939..19871,y=47816..73980,z=-66950..-37215
on x=42830..69488,y=-14019..4923,z=35706..67391
off x=-20419..-4039,y=-60731..-29107,z=-66968..-57202
on x=-51610..-18060,y=59855..88709,z=9677..20994
off x=-3664..9978,y=62233..91977,z=-30889..1856
off x=-55066..-41018,y=52826..84147,z=19145..21166
off x=-53786..-26173,y=-55709..-25301,z=-75810..-45581
off x=-43242..-10205,y=-71149..-62411,z=8579..31384
on x=47228..63188,y=-13425..-7478,z=58638..66252
off x=-34010..-18512,y=43627..65287,z=-70502..-49286
on x=54080..80201,y=25228..39727,z=-35590..-15128
on x=59537..79674,y=-43166..-18545,z=-42165..-29584
off x=-87846..-67717,y=3636..18154,z=2528..12702
off x=54261..71728,y=11913..16424,z=15694..49941
on x=59642..78694,y=6416..26070,z=-23394..956
on x=-66194..-47815,y=17012..30329,z=39584..40923
on x=-41565..-25844,y=-87690..-56844,z=5099..28342
off x=-86033..-65636,y=-1920..17124,z=31886..43921
on x=-95289..-78210,y=-7196..15386,z=-26432..-4501
on x=33029..47529,y=47252..64926,z=-57739..-33138
on x=-11072..20484,y=-50512..-21140,z=66373..72944
off x=-59649..-43750,y=46804..70953,z=-22519..-8585
off x=-65306..-44004,y=-61868..-47328,z=-10853..-7471
off x=53453..68177,y=51280..64448,z=7792..44549
on x=-66415..-38580,y=34310..51861,z=19400..54973
off x=-9778..20518,y=-75334..-58283,z=-47846..-35594
off x=-69272..-34920,y=-64545..-58275,z=6276..16442
off x=50458..83103,y=-37525..-14936,z=12129..45374
on x=-87935..-69022,y=-2922..13990,z=233..24934
off x=5625..30295,y=-75921..-38841,z=35883..53201
on x=-84537..-58666,y=-29866..-7544,z=-16451..-12030
off x=75797..96196,y=-20123..-8100,z=-15945..-3430
off x=-49987..-43558,y=-74791..-51841,z=-18619..10294
off x=-16381..9793,y=-50572..-14811,z=-72030..-52260
on x=69103..86864,y=-8857..220,z=-21868..-9718
off x=13588..43794,y=-24090..-3814,z=-82379..-69485
on x=41037..60568,y=51914..63732,z=-26989..1304
off x=-28615..2004,y=58752..66816,z=-57524..-43916
on x=-60196..-48673,y=25302..46869,z=-59779..-23119
off x=-98397..-64090,y=5002..18901,z=-3885..7925
off x=-7790..2948,y=53161..75104,z=-54347..-26993
off x=-31301..-2183,y=-64348..-48652,z=40092..62396
on x=32490..43043,y=-23390..-7716,z=-72232..-63204
off x=-41..11747,y=-5901..12293,z=-80015..-72779
off x=34891..58512,y=-62030..-31491,z=24679..59891
off x=-68529..-37764,y=-54048..-34524,z=-41393..-6441
on x=-80782..-72780,y=-11834..-1915,z=-800..27506
off x=-51263..-33945,y=-57028..-36886,z=-51163..-28816
on x=49570..68782,y=-8042..15479,z=-61841..-43569
on x=66860..92654,y=-2867..21306,z=-5367..8021
off x=64516..91537,y=-18107..7757,z=-28460..-12897
on x=-78948..-61199,y=27496..49823,z=19528..49188
off x=-55591..-31519,y=-71478..-51918,z=-21583..-10803
off x=53141..72697,y=1038..16528,z=-58614..-46373
on x=28755..52138,y=-79313..-51982,z=3905..27164
off x=-69986..-40601,y=5217..32192,z=-71122..-44492
off x=-1337..28493,y=50689..74836,z=-53096..-25638
on x=66667..86016,y=35548..48830,z=8672..27211
off x=-63856..-42419,y=-69369..-44552,z=-9920..811
off x=-10768..9966,y=-37641..-19032,z=58148..91112
on x=60150..79336,y=29848..36454,z=28059..41080
on x=9482..30859,y=30338..60185,z=55570..81577
on x=-32305..-6260,y=-72754..-49601,z=45214..58645
on x=-17341..282,y=75254..89586,z=-33014..-13326
on x=25344..50505,y=47887..58622,z=22986..42024
off x=-80270..-70075,y=-18567..3207,z=-38775..-21373
off x=-7668..9268,y=-36192..-13538,z=69119..94067
off x=65330..84703,y=-7495..12731,z=4339..31542
on x=6558..26020,y=606..13923,z=75632..79606
off x=-82539..-50372,y=-56323..-33011,z=5956..9113
on x=12181..33833,y=-41..27104,z=61223..85777
off x=41711..49279,y=-77556..-41007,z=18486..37108
on x=31531..41169,y=-78273..-56092,z=-42206..-35890
off x=-43904..-26416,y=-53410..-39215,z=-60465..-29898
off x=-89809..-61648,y=15721..27828,z=-10758..14455
on x=-41945..-22341,y=48457..79003,z=19316..37799
on x=-10737..10764,y=-61635..-47347,z=60444..81144
on x=5894..18357,y=-27040..-21700,z=-83764..-57458
on x=-74498..-40283,y=29367..42615,z=36861..52269
on x=59568..83428,y=17352..39563,z=-44286..-6552
on x=10360..39320,y=67270..81264,z=-12062..16520
on x=-86862..-51983,y=-36996..-17280,z=14010..15468
on x=-10324..10014,y=-95843..-61769,z=6292..13121
off x=-14829..1080,y=-75465..-59161,z=17227..51555
off x=-787..31028,y=-8098..8952,z=-80250..-59093
on x=48677..76613,y=-30342..-24431,z=-60910..-45967
on x=19796..25213,y=-59873..-28726,z=43894..65573
off x=-25612..-12949,y=25877..58548,z=54735..70223
off x=-31346..-19825,y=68072..88028,z=17470..38577
on x=18845..47373,y=-2474..16692,z=-89075..-57622
off x=-66873..-48255,y=-16142..-1145,z=38984..52754
off x=29024..41699,y=-57224..-32001,z=-51605..-48399
off x=64384..77726,y=-32840..-6155,z=-45931..-9008
off x=-42189..-18355,y=-76809..-50464,z=-12494..6913
on x=-15546..13881,y=-82755..-55415,z=9896..44536
off x=33536..46275,y=-77924..-58964,z=17876..32179
off x=-70126..-43775,y=31426..53399,z=-37304..-30987
off x=-2175..12636,y=16291..44089,z=72908..92770
off x=11084..24329,y=-35482..-133,z=62076..79455
off x=-71412..-53587,y=36438..55099,z=-7289..7960
on x=54639..77966,y=30602..53670,z=21088..44197
on x=70413..84784,y=21158..30637,z=-8992..19334
off x=-19699..10878,y=11948..32799,z=-79903..-66453
off x=-18449..5850,y=-80434..-57692,z=-37207..-17969
off x=36597..63492,y=-29260..-4817,z=49402..80059
off x=-21110..-1746,y=-58321..-45475,z=-71353..-42382
off x=-49681..-19143,y=-79377..-53603,z=20060..28560
on x=24960..40572,y=-34867..-2902,z=66507..72132
off x=-29769..-8143,y=37720..61740,z=41759..72292
on x=-33132..-22989,y=56342..59699,z=36681..59314
off x=-63754..-40076,y=44635..68144,z=2875..26560
off x=67249..89053,y=-18431..6400,z=-5847..12870
on x=-70244..-53781,y=-3118..19719,z=-61547..-33365
off x=19134..33348,y=-75833..-63094,z=21839..43019
off x=30577..60663,y=-21669..12886,z=57536..65077
on x=-74378..-50562,y=-40533..-18038,z=-47081..-24919
on x=-12380..5528,y=66594..79404,z=21246..42638
off x=7200..17423,y=58343..92465,z=-34023..-24260
off x=24674..32993,y=-63450..-26326,z=-78448..-50788
on x=-44331..-19065,y=-77371..-56526,z=26663..49869
on x=38039..61646,y=-69934..-47985,z=-30243..-10489
on x=-48936..-29136,y=-75338..-51409,z=379..11028
off x=-16311..14111,y=-38100..-30565,z=53095..73442
off x=-82989..-58173,y=-41391..-27482,z=23487..47863
on x=-61964..-45765,y=41407..69750,z=-42412..-21216
off x=31827..61487,y=-49559..-27881,z=-74602..-55080
off x=-42458..-21290,y=20529..38573,z=-75301..-59682
off x=-82527..-47895,y=-48153..-43393,z=-3185..24173
off x=-64743..-39431,y=46944..74191,z=26636..46368
off x=-45145..-19646,y=6330..22521,z=-73243..-52068
off x=24802..38827,y=-14580..12331,z=68191..79421
off x=-4982..-853,y=62190..81598,z=-12111..17877
off x=27170..31304,y=54077..75066,z=-42222..-28891
off x=-57717..-28034,y=50509..68284,z=-25440..710
on x=39908..53122,y=-15737..6830,z=55763..76451
off x=54434..57661,y=-58450..-49263,z=-3560..12231
off x=30526..48257,y=-35375..-17416,z=-71933..-48429
off x=68647..85672,y=-53099..-31157,z=-26232..6594
on x=-68520..-67118,y=12855..21932,z=31960..38748
off x=-89580..-67900,y=-10405..14610,z=12567..46469
off x=47424..84158,y=14545..46835,z=-40353..-27012
on x=2315..27981,y=-79190..-78416,z=-22019..14415
off x=63729..96079,y=-10936..12783,z=-11320..13651
off x=41734..65012,y=-62836..-45762,z=15001..27049
off x=24073..51213,y=24647..38935,z=55522..62263
off x=-56605..-43911,y=-28491..-15836,z=-72596..-43768
off x=-73713..-44594,y=40048..63675,z=1043..8542
on x=41320..57367,y=-51807..-20399,z=41008..47025
on x=-73821..-56756,y=28222..49790,z=-25247..-16793
off x=32161..66710,y=1857..28728,z=-77364..-40520
off x=-41036..-27292,y=22049..28519,z=67744..76329
on x=13527..35092,y=-39535..-24365,z=56377..70036
off x=-70137..-37611,y=-24509..-8532,z=57202..67742
on x=39807..55755,y=10601..32443,z=-68086..-50437
off x=-37020..-11107,y=-79773..-50762,z=27827..48699
off x=20430..35135,y=2362..25602,z=-84324..-59379
on x=-74640..-65651,y=4902..18156,z=32787..51162
on x=-44225..-26007,y=67721..89795,z=-17083..-2827
off x=-23754..3967,y=62633..86609,z=13865..33457
off x=-60583..-41519,y=-1047..12455,z=52556..73365
off x=-50992..-32598,y=16389..45404,z=-62485..-41273
off x=15311..44826,y=61278..74010,z=-55050..-34344
off x=14490..32695,y=70832..90434,z=5184..16444
on x=30484..62070,y=56490..73608,z=-43714..-22849
on x=46871..61357,y=-69427..-48446,z=-4288..13867
on x=-76779..-46596,y=-49541..-37133,z=3286..27263
off x=13477..50993,y=-81349..-64857,z=4744..11305
on x=63474..90005,y=12735..32459,z=-38155..-20233
on x=-75674..-40443,y=28889..48697,z=26683..38419
on x=-46985..-36144,y=53432..74680,z=-22168..3725
off x=-69357..-49593,y=-4107..21625,z=-73460..-56882
on x=-42033..-9137,y=61001..79202,z=4274..34898
on x=-7911..27236,y=48363..63805,z=-56831..-44785

View File

@@ -0,0 +1,62 @@
(define (load-instructions filename)
(call-with-input-file
filename
(lambda (file)
(let loop [(instructions '())]
(let [(instruction (get-datum file))]
(cond
[(eof-object? instruction)
(reverse instructions)]
[(symbol=? instruction 'inp)
(loop (cons (list instruction
(get-datum file)) instructions))]
[else
(loop (cons (list instruction
(get-datum file)
(get-datum file)) instructions))]))))))
(define (digits-from-number n)
(let loop [(n n) (digits '())]
(if (= n 0)
digits
(loop (div n 10) (cons (mod n 10) digits)))))
(define (interpret-with-input instructions input)
(let [(enviro '((x 0) (y 0) (z 0) (w 0)
(inp (lambda (n)
(let [(digit (car input))]
(set! input (cdr input))
digit)))
(add +)
(mul *)
(eql (lambda (a b) (if (= a b) 1 0)))))
(input (digits-from-number input))]
(eval (list 'let*
; base environment with input
(cons (list 'input (cons 'list input)) enviro)
; code adapted to be able to modify environment
(cons 'begin (map
(lambda (i)
(list 'set! (cadr i) i))
instructions))
; return value
'(list x y z w)))))
(define (trace instructions input)
(let loop [(already '())
(instructions instructions)]
(unless (null? instructions)
(printf "~20a: ~a~%"
(car instructions)
(interpret-with-input (cons (car instructions)
(reverse already))
input))
(loop (cons (car instructions) already)
(cdr instructions)))))
(define (biggest-valid-MONAD instructions)
0) ; no idea what x, y, z and w mean...
(let [(instructions (load-instructions "input"))]
(printf "part 1: ~% Biggest valid MONAD: ~a~%"
(biggest-valid-MONAD instructions)))

View File

@@ -0,0 +1,252 @@
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 12
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 11
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 12
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -7
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 11
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 2
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -1
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -16
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 11
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 10
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 2
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 10
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 0
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 0
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -4
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x 0
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 15
mul y x
add z y

View File

@@ -1,3 +1,27 @@
(import (srfi :43)) ; vector extensions
; get list of lines from a file
(define (get-lines file)
(let loop [(lines '())]
(if (eof-object? (peek-char file))
lines
(loop (cons (get-line file) lines)))))
; reads a numnber from input
; also consumes the next non-number character in the file!
(define (get-number file)
(let [(sign 1)]
(let loop [(index 0) (n 0)]
(let [(c (get-char file))]
(cond
[(char-numeric? c)
(loop (+ index 1) (+ (char->number c) (* n 10)))]
[(and (char=? c #\-)
(= index 0))
(set! sign -1)
(loop 1 n)]
[else (* sign n)])))))
; returns a list of numbers parsed from the first line of the file,
; separated by commas
(define (read-comma-separated-numbers file)
@@ -6,9 +30,99 @@
(cond
[(char=? c #\,) (loop (cons n draws) 0)]
[(char-whitespace? c) (reverse (cons n draws))]
[else (loop draws (+ (* n 10) (string->number (string c))))]))))
[else (loop draws (+ (* n 10) (char->number c)))]))))
; returns numbers 0 to 9 from ascii character
(define (char->number c)
(- (char->integer c) 48)) ; 48 is ASCII's number zero
(define-record-type matrix (fields width height data))
; builds a matrix record from the given 2D vector
; vectors inside data vector are expected to all be the same length
(define (matrix-from-data data)
(make-matrix
(vector-length (vector-ref data 0))
(vector-length data)
data))
; returns a matrix object of given dimensions full of zeroes
(define (filled-matrix width height filler)
(matrix-from-data
(do [(i 0 (+ i 1))
(v '() (cons (make-vector width filler) v))]
((>= i height) (list->vector v)) '())))
; returns value at x,y coordinates in matrix
; if coordinate is out of bounds, returns default
(define (matrix-get matrix x y default)
(if (or (< x 0)
(< y 0)
(>= x (matrix-width matrix))
(>= y (matrix-height matrix)))
default
(vector-ref (vector-ref (matrix-data matrix) y) x)))
; set value at x,y in matrix with given one
; if coordinate is out of bounds, nothing happens
(define (matrix-set! matrix x y value)
(when (and (>= x 0)
(>= y 0)
(< x (matrix-width matrix))
(< y (matrix-height matrix)))
(vector-set! (vector-ref (matrix-data matrix) y) x value)))
; print matrix line by line to console
; each line will be displayed as a scheme-vector
(define (matrix-print matrix)
(let y-loop [(y 0)]
(printf "~a~%" (vector-ref (matrix-data matrix) y))
(when (< y (- (matrix-height matrix) 1))
(y-loop (+ y 1)))))
; parse 0-9 numerical data from file and return a matrix
(define (load-matrix file)
(let y-loop [(heightmap '())]
(let x-loop [(line '())]
(let [(c (get-char file))]
(cond
[(eof-object? c)
(matrix-from-data (reverse-list->vector heightmap))]
[(char-whitespace? c)
(y-loop (cons (reverse-list->vector line) heightmap))]
[(char-numeric? c)
(x-loop (cons (char->number c) line))])))))
; matrix visitor
; calls function with all coordinates of matrix and value at corresponding point
; value returned by function is set at the visiting coordinate
(define (update-matrix! matrix function)
(let [(data (matrix-data matrix))
(width-1 (- (matrix-width matrix) 1))
(height-1 (- (matrix-height matrix) 1))]
(let y-loop [(y 0)]
(let x-loop [(x 0)]
(let [(value (vector-ref (vector-ref data y) x))]
(vector-set! (vector-ref data y) x
(function x y value))
(when (< x width-1)
(x-loop (+ x 1)))))
(when (< y height-1)
(y-loop (+ y 1))))))
; increase by 1 value at x,y in given matrix
; return that value too
(define (matrix-inc! matrix x y)
(let [(v (+ 1 (matrix-get matrix x y 0)))]
(matrix-set! matrix x y v)
v))
; sum all values of matrix
(define (matrix-sum matrix)
(vector-fold
(lambda (y sum line)
(+ sum
(vector-fold
(lambda (x sum value) (+ sum value))
0 line)))
0 (matrix-data matrix)))