Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

4
2021/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*\~
a.out
.gdb_history
*.link

3
2021/README.md Normal file
View file

@ -0,0 +1,3 @@
# Advent of Code 2021
My answers for [Advent of Code 2021](https://adventofcode.com/2021).

5
2021/day01/day01.bqn Normal file
View file

@ -0,0 +1,5 @@
a•BQN¨•Flines"input"
# Fn←{+´(¯1⊸↓<1⊸↓)+˝˘ 𝕨↕𝕩}
Fn{+´0>𝕨𝕩-𝕨𝕩}
•Show 1 Fn a # part 1
•Show 3 Fn a # part 2

23
2021/day01/day01.c Normal file
View file

@ -0,0 +1,23 @@
#include <stdio.h>
int main(void)
{
int buf[5000] = {0};
int count = 0;
while (scanf("%d\n", &buf[count++]) != EOF);
count--;
int part1 = 0;
for (int i = 1; i < count; ++i) {
part1 += buf[i] > buf[i - 1];
}
printf("%d\n", part1);
int part2 = 0;
for (int i = 3; i < count; ++i) {
part2 += buf[i] > buf[i-3];
}
printf("%d\n", part2);
return 0;
}

13
2021/day01/day01.scm Normal file
View file

@ -0,0 +1,13 @@
(import (chicken io) srfi-1)
(define (read-input #!optional (port (current-input-port)))
(map string->number
(read-lines port)))
(define (part1 l) (count < l (cdr l)))
(define (part2 l) (count < l (cdddr l)))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

7
2021/day01/day01_golf.c Normal file
View file

@ -0,0 +1,7 @@
b[5000],p,q,n,i;
main(){
for(;~scanf("%d",b+n++););
for(i=n-2;i--;)p+=b[i+1]>b[i];
for(i=n-4;i--;)q+=b[i+3]>b[i];
printf("%d %d",p,q);
}

25
2021/day01/day01_r7rs.scm Normal file
View file

@ -0,0 +1,25 @@
(cond-expand
(r7rs)
(chicken (import r7rs)))
(import (scheme base)
(scheme write)
(scheme cxr)
(srfi 1))
(define (read-input)
(let lp ((lines '()))
(if (and (not (null? lines)) (eof-object? (car lines)))
(map string->number (reverse (cdr lines)))
(lp (cons (read-line (current-input-port)) lines)))))
(define (part1 l) (count < l (cdr l)))
(define (part2 l) (count < l (cdddr l)))
(let ((in (read-input)))
(write (part1 in))
(newline)
(write (part2 in))
(newline))

2000
2021/day01/input Normal file

File diff suppressed because it is too large Load diff

6
2021/day02/day02.awk Normal file
View file

@ -0,0 +1,6 @@
/down/ {aim += $2}
/up/ {aim -= $2}
/forward/ {pos += $2; depth += aim * $2}
END {print pos * aim, pos * depth}
# one-liner
# /^d/{a+=$2}/u/{a-=$2}/f/{p+=$2;d+=a*$2}END{print p*a,p*d}

32
2021/day02/day02.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
int main() {
char instr[9] = {0};
int n = 0;
int pos = 0;
int depth = 0;
int aim = 0;
while (scanf("%s %d\n", instr, &n) == 2) {
switch (instr[0]) {
case 'd':
aim += n;
break;
case 'u':
aim -= n;
break;
case 'f':
pos += n;
depth += aim * n;
break;
default:
fprintf(stderr, "unknown instruction: %s", instr);
break;
}
}
printf("%d\n", pos * aim);
printf("%d\n", pos * depth);
return 0;
}

30
2021/day02/day02.scm Normal file
View file

@ -0,0 +1,30 @@
(import (chicken io)
srfi-1
srfi-152
matchable)
(define (read-input #!optional (port (current-input-port)))
(map (lambda (s) (let ((l (string-split s " ")))
(list (string->symbol (car l))
(string->number (cadr l)))))
(read-lines port)))
(define (move steps pos depth aim)
(match steps
(() (values pos depth aim))
((('down n) . rest) (move rest pos depth (+ aim n)))
((('up n) . rest) (move rest pos depth (- aim n)))
((('forward n) . rest) (move rest (+ pos n) (+ depth (* aim n)) aim))
(_ (error "unknown instruction" (car steps)))))
(define (part1 l)
(let-values (((pos depth aim) (move l 0 0 0)))
(* pos aim)))
(define (part2 l)
(let-values (((pos depth aim) (move l 0 0 0)))
(* pos depth)))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

1
2021/day02/day02_golf.c Normal file
View file

@ -0,0 +1 @@
c,n,p,d,a;main(){while(scanf("%c%*s%d\n",&c,&n)>1)c^'d'?a+=n:c^'f'?p+=n,d+=a*n:a-=n;printf("%d %d",p*a,p*d);}

1000
2021/day02/input Normal file

File diff suppressed because it is too large Load diff

3
2021/day03/day03.bqn Normal file
View file

@ -0,0 +1,3 @@
in-'0'¨•FLines"input"
D2B2×+˜´
•Show ×´D2B¨(1-)(+´>÷2˙)in # part 1

35
2021/day03/day03.c Normal file
View file

@ -0,0 +1,35 @@
#include <stdio.h>
#define N 1000
#define M 12
int main() {
char c = 0;
int i = 0, j = 0;
char in[N][M] = {0};
while ((c = getchar()) != EOF) {
switch (c) {
case '\n': i++; j = 0; break;
case '1': in[i][j] = 1; j++; break;
case '0': j++; break;
default: fprintf(stderr, "unknown digit %c", c); return 1;
}
}
// Part 1
int counts[M] = {0};
for (i = 0; i < N; ++i) {
for (j = 0; j < M; ++j) {
counts[j] += in[i][j];
}
}
int gamma = 0, epsilon = 0;
for (j = 0; j < M; ++j) {
gamma = (gamma << 1) + (counts[j] > N/2);
epsilon = (epsilon << 1) + (counts[j] <= N/2);
}
printf("%d\n", gamma * epsilon);
return 0;
}

47
2021/day03/day03.scm Normal file
View file

@ -0,0 +1,47 @@
(import (chicken io)
(chicken bitwise)
srfi-1
srfi-13)
(define (binary-string->list s)
(map (lambda (c) (- (char->integer c) (char->integer #\0)))
(string->list s)))
(define (list->binary-string l)
(string-concatenate (map number->string l)))
(define (read-input #!optional (port (current-input-port)))
(map binary-string->list (read-lines port)))
(define (part1 l)
(let* ((n (/ (length l) 2))
(counts (apply map + l))
(most-common (map (lambda (x) (if (> x n) 1 0)) counts))
(gamma (string->number (list->binary-string most-common) 2))
(epsilon (string->number (list->binary-string (map (lambda (d) (- 1 d)) most-common)) 2)))
(* gamma epsilon)))
(define (o2-rating numbers i)
(if (= 1 (length numbers))
(car numbers)
(let* ((digits (map (lambda (l) (list-ref l i)) numbers))
(most-common (if (>= (apply + digits) (/ (length numbers) 2)) 1 0))
(new-numbers (filter (lambda (n) (= (list-ref n i) most-common)) numbers)))
(o2-rating new-numbers (+ 1 i)))))
(define (co2-rating numbers i)
(if (= 1 (length numbers))
(car numbers)
(let* ((digits (map (lambda (l) (list-ref l i)) numbers))
(least-common (if (>= (apply + digits) (/ (length numbers) 2)) 0 1))
(new-numbers (filter (lambda (n) (= (list-ref n i) least-common)) numbers)))
(co2-rating new-numbers (+ 1 i)))))
(define (part2 l)
(let ((o2 (string->number (list->binary-string (o2-rating l 0)) 2))
(co2 (string->number (list->binary-string (co2-rating l 0)) 2)))
(* o2 co2)))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

1000
2021/day03/input Normal file

File diff suppressed because it is too large Load diff

55
2021/day05/day05.scm Normal file
View file

@ -0,0 +1,55 @@
(import (chicken io)
srfi-1
srfi-42
srfi-69
srfi-152
matchable)
(define (read-input #!optional (port (current-input-port)))
(define (parse-entry s)
(map (lambda (x) (map string->number (string-split x ",")))
(string-split s " -> ")))
(map parse-entry (read-lines port)))
(define (horizontal? line)
(match-let ((((x1 y1) (x2 y2)) line))
(= x1 x2)))
(define (vertical? line)
(match-let ((((x1 y1) (x2 y2)) line))
(= y1 y2)))
(define (diagonal? line)
(and (not (horizontal? line))
(not (vertical? line))))
(define (range x1 x2)
(iota (+ 1 (abs (- x2 x1)))
x1
(if (>= x2 x1) 1 -1)))
(define (all-points lines #!optional (diagonals #f))
(define h (make-hash-table #:initial 0))
(do-ec (: line lines)
(or (horizontal? line) (vertical? line))
(match-let ((((x1 y1) (x2 y2)) line))
(list-ec (: x (range x1 x2))
(: y (range y1 y2))
(hash-table-update! h (list x y) add1))))
(when diagonals
(do-ec (: line lines)
(if (diagonal? line))
(match-let ((((x1 y1) (x2 y2)) line))
(list-ec (:parallel (: x (range x1 x2))
(: y (range y1 y2)))
(hash-table-update! h (list x y) add1)))))
h)
(define (part12 lines diagonals)
(length
(filter (lambda (n) (> n 1))
(hash-table-values (all-points lines diagonals)))))
(let ((lines (read-input)))
(print (part12 lines #f))
(print (part12 lines #t)))

500
2021/day05/input Normal file
View file

@ -0,0 +1,500 @@
556,286 -> 341,71
337,201 -> 782,646
786,780 -> 117,111
977,864 -> 199,86
544,127 -> 544,144
539,471 -> 539,972
364,765 -> 364,285
282,325 -> 892,935
536,941 -> 158,941
280,39 -> 806,565
168,662 -> 363,857
639,676 -> 639,701
397,809 -> 466,809
716,118 -> 625,209
153,582 -> 497,926
35,706 -> 398,706
61,41 -> 519,41
158,557 -> 158,922
320,177 -> 624,481
800,779 -> 466,779
292,270 -> 292,497
919,17 -> 757,17
935,520 -> 935,688
948,480 -> 303,480
306,811 -> 306,467
227,582 -> 464,819
757,807 -> 757,688
983,12 -> 30,965
632,262 -> 640,262
755,314 -> 138,931
179,599 -> 179,144
556,246 -> 801,491
114,218 -> 114,60
183,70 -> 423,70
561,661 -> 231,331
875,738 -> 723,890
812,828 -> 812,78
98,707 -> 88,707
804,979 -> 188,979
503,178 -> 453,178
902,678 -> 248,678
603,618 -> 737,752
788,735 -> 559,964
839,134 -> 839,408
569,356 -> 491,356
566,543 -> 566,77
624,386 -> 111,386
781,109 -> 781,52
187,709 -> 23,545
220,123 -> 856,123
210,528 -> 398,716
942,935 -> 58,51
102,13 -> 102,560
926,588 -> 969,545
529,117 -> 146,117
989,211 -> 739,461
207,201 -> 207,106
799,876 -> 284,876
370,773 -> 687,456
571,972 -> 571,351
877,910 -> 877,12
384,205 -> 384,940
493,915 -> 912,496
764,412 -> 368,16
368,220 -> 333,220
526,271 -> 778,271
379,203 -> 417,165
168,577 -> 168,709
529,808 -> 598,739
959,506 -> 959,626
576,89 -> 863,89
277,412 -> 311,378
12,19 -> 975,982
620,951 -> 240,571
937,206 -> 954,206
231,177 -> 918,864
499,825 -> 315,825
289,876 -> 289,302
408,569 -> 46,207
461,838 -> 91,838
281,294 -> 281,737
61,541 -> 61,747
768,857 -> 768,276
782,97 -> 33,97
544,563 -> 251,856
731,216 -> 731,19
767,142 -> 242,667
469,612 -> 952,129
508,363 -> 508,540
614,845 -> 614,739
342,362 -> 235,362
880,703 -> 880,653
942,669 -> 651,669
884,976 -> 884,924
158,822 -> 945,35
510,716 -> 884,342
523,946 -> 73,496
334,430 -> 157,253
593,77 -> 105,565
269,132 -> 243,132
902,529 -> 180,529
554,767 -> 554,938
164,615 -> 425,615
51,713 -> 51,341
428,606 -> 89,945
600,402 -> 600,185
774,550 -> 774,207
465,204 -> 697,436
577,719 -> 255,719
647,990 -> 970,990
649,170 -> 886,407
428,503 -> 428,394
381,608 -> 381,444
778,175 -> 778,121
795,379 -> 379,379
929,792 -> 218,81
504,837 -> 504,449
212,216 -> 212,76
446,744 -> 446,116
824,247 -> 577,247
77,214 -> 553,214
913,234 -> 913,670
949,24 -> 43,930
733,758 -> 733,62
628,659 -> 962,659
172,749 -> 755,749
901,717 -> 184,717
457,578 -> 923,112
943,11 -> 912,11
728,597 -> 116,597
465,134 -> 465,159
170,953 -> 170,533
231,715 -> 231,219
209,187 -> 984,962
798,515 -> 798,601
479,123 -> 479,148
360,387 -> 360,356
962,818 -> 962,770
852,607 -> 852,886
159,838 -> 967,30
823,659 -> 642,478
374,893 -> 545,893
248,819 -> 248,978
894,473 -> 894,47
182,975 -> 278,879
75,248 -> 913,248
969,533 -> 969,827
18,40 -> 842,864
972,909 -> 220,157
378,159 -> 571,159
875,478 -> 238,478
95,807 -> 264,638
418,68 -> 418,387
784,548 -> 332,548
365,354 -> 365,836
731,615 -> 235,615
885,104 -> 513,476
816,47 -> 385,478
626,741 -> 626,499
371,372 -> 920,921
83,150 -> 922,989
623,520 -> 645,498
612,305 -> 561,305
845,149 -> 788,149
914,35 -> 829,35
143,165 -> 143,520
164,218 -> 266,218
118,644 -> 397,644
59,942 -> 970,31
616,774 -> 970,420
30,468 -> 874,468
454,208 -> 454,536
524,488 -> 524,931
54,479 -> 560,479
815,591 -> 815,813
959,971 -> 30,42
23,181 -> 149,181
841,294 -> 841,681
34,47 -> 367,47
913,590 -> 913,374
690,64 -> 690,672
541,112 -> 781,112
380,843 -> 687,536
303,330 -> 465,330
408,403 -> 326,403
352,962 -> 925,389
121,882 -> 873,130
979,294 -> 29,294
228,688 -> 228,738
845,930 -> 901,930
726,189 -> 27,888
223,888 -> 989,888
483,632 -> 483,321
606,810 -> 820,810
225,31 -> 225,342
841,18 -> 841,417
375,185 -> 375,413
641,189 -> 307,523
126,900 -> 126,990
530,220 -> 690,220
496,263 -> 154,263
140,503 -> 419,503
349,733 -> 349,819
43,29 -> 928,914
683,842 -> 683,489
113,634 -> 806,634
771,145 -> 130,145
88,467 -> 908,467
328,642 -> 328,795
986,191 -> 218,959
857,166 -> 857,594
950,763 -> 229,42
263,940 -> 101,940
689,182 -> 689,835
241,237 -> 733,237
965,150 -> 279,150
871,242 -> 474,639
688,947 -> 688,11
319,738 -> 945,112
21,853 -> 853,21
69,533 -> 69,741
492,981 -> 492,210
942,69 -> 249,69
63,364 -> 203,364
340,505 -> 15,505
41,43 -> 979,981
395,623 -> 217,801
540,37 -> 540,381
64,112 -> 882,930
887,212 -> 217,882
168,159 -> 108,159
117,22 -> 959,864
413,500 -> 413,616
775,597 -> 962,597
171,901 -> 143,901
777,391 -> 41,391
901,139 -> 70,970
215,75 -> 215,261
973,433 -> 786,433
757,568 -> 612,423
363,347 -> 185,525
274,363 -> 274,709
435,569 -> 880,569
267,297 -> 86,478
221,852 -> 985,88
322,560 -> 322,962
470,259 -> 470,508
861,860 -> 843,860
172,474 -> 172,714
53,839 -> 499,839
600,40 -> 600,227
820,952 -> 99,231
650,486 -> 586,486
305,273 -> 305,392
826,417 -> 826,92
309,934 -> 309,720
381,644 -> 381,623
38,78 -> 38,54
326,450 -> 173,450
474,100 -> 474,135
607,536 -> 192,121
686,504 -> 164,504
538,623 -> 429,623
200,385 -> 933,385
568,275 -> 31,275
105,201 -> 706,201
582,584 -> 827,584
24,469 -> 24,519
306,224 -> 32,224
429,528 -> 304,528
272,851 -> 272,927
636,113 -> 636,244
481,107 -> 783,107
834,87 -> 175,746
684,50 -> 61,673
30,335 -> 739,335
621,893 -> 266,893
968,942 -> 968,390
895,23 -> 136,23
742,650 -> 756,636
42,582 -> 368,582
890,266 -> 786,266
591,807 -> 921,807
915,333 -> 915,160
746,326 -> 826,326
663,803 -> 34,174
533,513 -> 692,513
205,133 -> 935,133
730,138 -> 58,810
290,87 -> 290,488
693,513 -> 693,323
188,491 -> 188,587
562,593 -> 562,122
629,457 -> 629,299
132,781 -> 381,781
356,965 -> 356,899
720,715 -> 487,715
356,120 -> 954,120
657,507 -> 323,173
13,190 -> 742,190
677,640 -> 491,640
145,605 -> 366,605
143,683 -> 681,145
700,787 -> 557,787
958,406 -> 212,406
267,734 -> 705,734
470,333 -> 257,120
790,656 -> 523,389
13,904 -> 898,19
29,970 -> 961,38
846,454 -> 846,153
564,488 -> 98,488
904,19 -> 60,863
493,112 -> 472,133
945,977 -> 141,173
720,231 -> 720,367
783,133 -> 783,422
165,754 -> 165,604
752,308 -> 715,271
413,969 -> 431,951
833,437 -> 833,881
612,802 -> 612,64
974,187 -> 543,618
655,183 -> 675,183
696,833 -> 906,623
756,792 -> 756,741
338,140 -> 878,680
854,955 -> 241,342
602,466 -> 326,466
470,125 -> 464,131
568,141 -> 43,666
826,318 -> 783,275
194,986 -> 194,466
896,330 -> 621,55
482,709 -> 704,931
345,912 -> 345,741
758,119 -> 758,841
11,777 -> 11,249
88,945 -> 795,945
665,74 -> 124,615
243,831 -> 249,837
40,69 -> 720,749
757,804 -> 757,900
803,265 -> 336,732
299,155 -> 758,614
787,173 -> 172,788
251,400 -> 251,168
217,480 -> 486,480
939,974 -> 21,56
767,649 -> 378,649
197,764 -> 561,400
767,577 -> 579,577
952,982 -> 28,58
282,527 -> 282,640
944,125 -> 184,125
149,848 -> 351,848
36,437 -> 350,437
63,527 -> 764,527
66,313 -> 302,549
805,485 -> 577,485
660,626 -> 903,626
927,542 -> 897,542
577,344 -> 577,934
624,284 -> 624,497
649,618 -> 153,122
942,32 -> 227,747
10,190 -> 10,629
84,638 -> 470,252
362,89 -> 362,762
351,844 -> 916,279
683,561 -> 497,747
628,473 -> 103,473
319,525 -> 782,62
842,131 -> 551,131
980,960 -> 51,31
662,12 -> 666,12
337,814 -> 337,736
720,99 -> 760,99
867,515 -> 867,650
248,872 -> 142,872
295,274 -> 298,274
102,369 -> 102,648
523,142 -> 54,611
369,798 -> 978,189
215,688 -> 835,688
846,242 -> 786,182
68,923 -> 68,342
690,416 -> 559,547
567,134 -> 278,134
89,126 -> 846,883
779,325 -> 389,325
675,461 -> 675,622
278,925 -> 953,250
907,460 -> 519,848
769,60 -> 592,60
331,103 -> 331,49
148,366 -> 148,516
933,52 -> 332,52
488,642 -> 488,523
632,403 -> 83,952
321,840 -> 756,405
302,627 -> 907,22
650,449 -> 650,553
219,466 -> 219,297
841,947 -> 78,184
40,746 -> 712,74
559,306 -> 895,306
317,592 -> 317,275
267,183 -> 267,428
43,397 -> 43,359
952,705 -> 952,634
921,837 -> 258,174
634,783 -> 656,805
366,309 -> 224,309
383,470 -> 526,613
717,419 -> 717,332
543,752 -> 543,500
954,892 -> 101,39
294,379 -> 583,379
829,388 -> 829,491
748,509 -> 283,509
347,75 -> 467,195
618,958 -> 371,711
986,827 -> 213,54
34,617 -> 982,617
716,902 -> 716,429
970,52 -> 107,915
563,33 -> 563,680
803,82 -> 136,749
24,968 -> 273,968
816,483 -> 620,287
783,588 -> 623,588
397,210 -> 988,801
911,387 -> 911,446
770,730 -> 786,730
22,11 -> 982,971
395,316 -> 151,316
301,420 -> 301,248
10,10 -> 989,989
637,358 -> 247,358
932,341 -> 642,341
162,594 -> 162,448
51,946 -> 396,946
591,253 -> 958,620
567,849 -> 567,713
879,910 -> 879,603
889,642 -> 437,642
669,528 -> 945,252
644,237 -> 774,237
488,870 -> 738,620
692,388 -> 959,388
506,17 -> 701,17
663,514 -> 663,216
684,862 -> 289,862
511,235 -> 519,227
866,940 -> 153,227
381,518 -> 87,518
837,573 -> 181,573
337,191 -> 337,135
324,573 -> 945,573
449,800 -> 390,741
763,378 -> 763,695
24,457 -> 444,877
267,875 -> 798,344
724,848 -> 395,848
931,322 -> 931,244
426,241 -> 280,241
175,879 -> 175,883
496,158 -> 560,158
899,319 -> 805,319
799,424 -> 563,188
958,388 -> 958,290
558,95 -> 314,95
768,646 -> 961,839
246,534 -> 246,147
808,720 -> 808,385
912,147 -> 912,305
670,676 -> 776,676
534,594 -> 696,594
736,768 -> 736,364
377,784 -> 377,368
799,105 -> 978,284
763,575 -> 763,253
581,205 -> 581,45
932,782 -> 678,782
400,950 -> 936,414
68,616 -> 897,616
399,662 -> 291,554
354,397 -> 354,489
219,276 -> 862,919
115,138 -> 195,138
863,326 -> 863,335
884,130 -> 271,743
986,148 -> 234,900
254,186 -> 973,905
975,971 -> 672,971
122,533 -> 19,636
316,512 -> 219,609
113,480 -> 716,480
483,540 -> 845,178

10
2021/day05/test Normal file
View file

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

36
2021/day06/day06.scm Normal file
View file

@ -0,0 +1,36 @@
(import (chicken io)
srfi-1
srfi-42
srfi-152)
(define (read-input #!optional (port (current-input-port)))
(map string->number (string-split (read-line port) ",")))
(define (count-fishes in)
(define c (make-list 9 0))
(do-ec (: x in)
(set! (list-ref c x) (add1 (list-ref c x))))
c)
(define (simulate counts days)
(if (= 0 days)
(take counts 9)
(let ((n (car counts)))
(set! (list-ref counts 7) (+ n (list-ref counts 7)))
(simulate (cdr counts) (- days 1)))))
(define (total-fishes counts days)
(apply + (take (simulate (apply circular-list counts) days) 9)))
(let* ((in '(3 4 3 1 2))
(counts (count-fishes in)))
(print "Test:")
(print (total-fishes counts 18))
(print (total-fishes counts 80))
(print (total-fishes counts 256)))
(print)
(let* ((in (read-input))
(counts (count-fishes in)))
(print (total-fishes counts 80))
(print (total-fishes counts 256)))

1
2021/day06/input Normal file
View file

@ -0,0 +1 @@
3,3,2,1,4,1,1,2,3,1,1,2,1,2,1,1,1,1,1,1,4,1,1,5,2,1,1,2,1,1,1,3,5,1,5,5,1,1,1,1,3,1,1,3,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,4,1,3,3,1,1,3,1,3,1,2,1,3,1,1,4,1,2,4,4,5,1,1,1,1,1,1,4,1,5,1,1,5,1,1,3,3,1,3,2,5,2,4,1,4,1,2,4,5,1,1,5,1,1,1,4,1,1,5,2,1,1,5,1,1,1,5,1,1,1,1,1,3,1,5,3,2,1,1,2,2,1,2,1,1,5,1,1,4,5,1,4,3,1,1,1,1,1,1,5,1,1,1,5,2,1,1,1,5,1,1,1,4,4,2,1,1,1,1,1,1,1,3,1,1,4,4,1,4,1,1,5,3,1,1,1,5,2,2,4,2,1,1,3,1,5,5,1,1,1,4,1,5,1,1,1,4,3,3,3,1,3,1,5,1,4,2,1,1,5,1,1,1,5,5,1,1,2,1,1,1,3,1,1,1,2,3,1,2,2,3,1,3,1,1,4,1,1,2,1,1,1,1,3,5,1,1,2,1,1,1,4,1,1,1,1,1,2,4,1,1,5,3,1,1,1,2,2,2,1,5,1,3,5,3,1,1,4,1,1,4

35
2021/day07/day07.scm Normal file
View file

@ -0,0 +1,35 @@
(import (chicken io)
(chicken sort)
srfi-1
srfi-152)
(define (read-input #!optional (port (current-input-port)))
(map string->number (string-split (read-line port) ",")))
(define (median lst)
(list-ref (sort lst <=) (/ (length lst) 2)))
(define (part1 in)
(let ((target-pos (median in)))
(apply + (map (lambda (x) (abs (- x target-pos))) in))))
(define (cost start end)
(let ((dist (abs (- start end))))
(/ (* dist (+ dist 1)) 2)))
(define (total-fuel target-pos in)
(apply + (map (lambda (x) (cost target-pos x)) in)))
(define (part2 in)
(apply min
(let ((min-in (apply min in))
(max-in (apply max in)))
(let lp ((pos min-in)
(fuels '()))
(if (> pos max-in)
fuels
(lp (+ 1 pos) (cons (total-fuel pos in) fuels)))))))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

1
2021/day07/input Normal file
View file

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,209,573,1277,704,518,276,196,62,1226,170,58,1450,101,65,99,435,986,1437,1570,35,354,247,110,105,139,1209,23,1074,339,69,483,21,33,323,1348,111,2,270,1239,316,529,1680,1056,1960,257,1009,1073,59,425,1181,198,31,299,771,53,817,728,931,72,517,39,279,304,401,1271,533,1551,133,297,162,902,370,985,643,1217,78,16,380,223,177,600,349,12,776,26,1738,526,85,1542,111,844,93,595,1545,873,836,422,180,1187,329,231,1521,54,162,212,471,1329,156,1299,160,541,676,67,200,22,24,76,242,178,1093,1173,818,1380,284,335,642,1047,112,271,541,927,52,983,238,116,135,871,400,436,1094,684,249,263,303,24,437,813,32,45,19,620,57,866,44,68,277,1112,110,77,1481,437,302,678,541,904,322,13,186,1474,836,43,1020,201,1586,1169,1149,470,535,55,879,133,1229,106,989,1023,256,103,56,401,667,557,98,288,694,286,237,1661,933,1063,20,227,80,815,289,1414,234,517,227,616,829,191,1211,92,591,279,22,139,67,214,60,145,468,10,521,807,1243,76,163,190,122,804,88,383,319,1127,399,376,423,304,126,10,297,377,1103,691,139,70,519,16,15,43,397,468,1183,90,28,1262,151,1448,62,64,1072,386,1330,1313,12,100,657,28,55,612,337,1865,704,263,565,249,564,565,1218,40,1146,150,718,1253,228,120,713,925,159,36,1087,1023,1490,316,540,1124,1127,781,417,656,0,174,1006,529,389,86,90,78,403,1500,253,35,655,650,933,815,108,168,321,345,147,251,258,25,173,243,740,48,476,1507,634,425,738,160,1415,395,448,156,636,1967,516,316,628,810,817,26,20,753,22,1133,352,204,211,47,22,874,43,12,18,1015,779,108,579,251,1398,33,1507,93,274,904,221,1062,868,3,363,42,14,435,62,1508,540,64,267,1690,418,205,502,152,142,414,178,50,344,780,81,635,128,355,239,1708,1814,29,251,624,22,38,789,948,186,529,895,76,150,416,502,975,1216,456,862,522,1149,131,10,121,1353,313,568,595,6,318,633,331,1652,656,214,21,35,289,80,860,229,244,1188,350,594,424,235,327,6,1083,40,134,839,279,172,1452,197,47,2,73,607,238,1151,844,533,110,1207,125,129,16,1000,965,236,228,497,589,111,1245,453,179,956,116,212,47,497,380,574,355,799,209,384,47,449,688,312,748,1531,1092,23,1001,69,155,924,1352,163,1561,743,609,1261,1231,32,1,739,513,300,370,36,568,89,487,201,11,146,274,163,1029,829,469,299,118,732,769,120,1093,776,610,1944,90,67,494,831,88,227,1257,344,662,401,310,664,56,94,183,935,179,643,4,1083,567,1525,208,204,899,123,36,438,1171,265,1406,177,202,1398,631,444,385,589,29,124,96,237,374,793,794,502,665,287,575,113,305,157,465,376,66,662,77,595,75,141,243,254,30,5,622,140,443,566,360,192,1531,1113,1299,598,147,469,732,1565,409,1380,550,173,232,361,131,99,37,547,132,1779,193,228,664,553,568,389,1069,58,71,610,738,624,261,491,158,105,416,131,198,35,823,9,313,6,429,1492,290,313,272,281,427,280,661,141,54,383,3,130,43,418,2,1040,1051,1006,38,151,1325,1357,117,1473,175,201,613,1458,1218,588,169,228,565,901,420,42,117,110,442,9,99,1685,979,84,35,129,248,1,21,360,123,203,1320,1200,209,510,362,106,148,313,292,63,842,93,88,134,720,565,156,118,983,119,1451,757,736,445,466,226,265,573,612,652,170,225,32,1049,1332,366,1375,692,270,388,321,1153,909,1266,93,5,495,377,212,429,90,199,278,631,693,63,816,395,281,315,0,737,575,121,865,1,485,262,49,804,518,109,600,358,221,14,370,450,947,448,67,576,22,1266,226,100,10,607,620,295,568,316,51,687,199,1478,45,489,1878,1035,298,219,363,85,664,1290,492,70,644,78,163,100,102,465,732,439,93,25,847,297,172,361,393,304,461,583,122,121,762,58,112,85,142,48,193,1617,386,685,1054,584,488,394,665,277,263,596,290,1231,171,1394,9,1218,77,54,487,182,528,695,662,413,345,51,690,1702,203,1500,461,1755,190,371,1122,1614,324,238,569,1482,15,711,1332,700,437,242,174,642,660,987,1232,121,620,17,389,22,105,847,36,251,285,1238,162,1227,1473,411,66,258,377,1135,438,117,664,281,1070,301,132,256,498,172,194,103,662,606,342,340,1501,802,549,380,58,179,361

65
2021/day09/day09.scm Normal file
View file

@ -0,0 +1,65 @@
(import (chicken io)
(chicken sort)
srfi-1
srfi-42
srfi-152
generalized-arrays
storage-classes)
(define (string->list-of-ints s)
(map (lambda (x) (- x 48)) (map char->integer (string->list s))))
(define (read-input #!optional (port (current-input-port)))
(nested-list->array
(map string->list-of-ints (read-lines port))
s8vector-storage-class
2))
(define (neighbours grid idx)
(let ((m (vector-ref (array-shape grid) 0))
(n (vector-ref (array-shape grid) 1))
(i (vector-ref idx 0))
(j (vector-ref idx 1)))
(list-ec (:range k (- i 1) (+ i 2))
(:range l (- j 1) (+ j 2))
(and (not (and (= k i) (= l j)))
(or (= k i) (= l j))
(>= k 0) (>= l 0) (< k m) (< l n))
(list (vector k l) (array-ref grid (vector k l))))))
(define (low-points grid)
(let ((m (vector-ref (array-shape grid) 0))
(n (vector-ref (array-shape grid) 1)))
(list-ec (:range i m)
(:range j n)
(:let x (array-ref grid (vector i j)))
(:let neighb (map cadr (neighbours grid (vector i j))))
(if (every (lambda (n) (< x n)) neighb))
(list (vector i j) x))))
(define (part1 grid)
(apply + (map add1 (map cadr (low-points grid)))))
(define (compute-basins grid)
(define n (vector-ref (array-shape grid) 0))
(define m (vector-ref (array-shape grid) 1))
(define visited (make-array vector-storage-class (vector n m) #f))
(define (basin-size idx)
(sum-ec (: neighbour (neighbours grid idx))
(:let neighbour-idx (car neighbour))
(:let neighbour-val (cadr neighbour))
(not (array-ref visited neighbour-idx))
(begin
(array-set! visited neighbour-idx #t)
(if (= neighbour-val 9)
0
(add1 (basin-size neighbour-idx))))))
(list-ec (: lowp-idx (map car (low-points grid)))
(basin-size lowp-idx)))
(define (part2 grid)
(apply * (take (sort (compute-basins grid) >) 3)))
(let ((grid (read-input)))
(print (part1 grid))
(print (part2 grid)))

100
2021/day09/input Normal file
View file

@ -0,0 +1,100 @@
7659991098999876579910129879999876432123459874345567890126678999876588975767899323456989767899432101
8998789987898765467891239868899876541012398765123456789234567897643467894656798912399878948678944212
9867678976789878598954398756789997632343459873234569898765679999856578943547987893989865434567894323
7654567895896989679767499847994398755456579987656778969876892198767989652129876889876976725678965734
8767678954345698799898987659943219876577694598787889545987931019879996543298965679965987438789876799
9898789921239799898989899798794399987688965679898993534598942123989987654987654567894596549899989987
4969999892398989987876789987689989998789898789979992123989653934598798965976543678943987678999999876
3459898789497878976545678986567878999895679898768989239879869896789659879865432469432198799998789765
2498765699976567995434389765434567899934989977655679356965998789896545989865321258921019999987678954
3987654398765456789321238979325456799329898766434798999876797698987432198754310347894329789876567893
4696543219876967998910147998214345678998789954323987689999977567898543479885541456789498678988678932
5987654423987878987521236987601234567891678893219876567898765479987654567976632367899987569899789321
6798767834698989997432345698524568698932456789398765466989876567898765689987545478959768456789893210
7899898945679499876545656997434578799843569892987654345678989679999876799798658569349654367878964322
8977999498789398987896769876545689998767678931999869656789698789999987987698767895498743212567895433
9656789329898987899929879987676790199898989949877998767896559899878998977569978999987654323458986654
8797995434987896989434989798989891987919499896765569888921434998767789865452989998998795434569997765
9979899549876785678945995639698999876329398764354456999990125899545678954321299987899987895678949878
9865678998765434567899894324567892985498999863212347898989436798434599876210389996789999998789434999
7654567899896647978989789212459921296987899954393478987678945987324879965341567895679891019998959865
8543878901987656789765678901268933459876999895989569658567959876412568897432467894599789923987898654
9212389919898767897654577892999545998765798789878978945456899954323456789576578923989679899876789543
9543457898769879986543456789889959876543987676567899432345789976436567897697989219878565678965678932
8754568987654989765432347898767899989432976543456964321012498987545679998989892109867434699764567891
9867689298543299986541034987656789998743989654677895632134987898696789989878789298754324789543488989
1978792129654569987732129874543567897654799965789976853239876799989999878765678999865455697601245678
0989893098765678998653298763212375789795679878994989966398754889879898765464569899976566789212346789
9898954239987789998784987654301234699989789989873398765459765678968789884323456789987677894323456898
8777895345698999899895698976214345789878999997762129876589896789345678965434567999898989976564568967
7656976457899019767987899765423456789569899876543299987678987891234569876545898998769394987875679456
6546899568992198657898939878534677893456789987654989898789398932347678999856789889843212398986989345
5435688979879932545679929989665788902569991298779878789899299543458789798767895678932101569997890123
4323567899767891434589898998776899543457899999898768655978987654569899679878934569643219878998921254
6764579987658910123456797879887899656568978789987653234567998785678998532989545678954399989999432765
7875689998767891234567896569998998967989767698798767145678999896789987643498756789765989992987543876
8976796899878932545678997998769467899899854599659898657789985959897898784569867899876978931098954987
9697895799989873467889989999652348999799965678943939768999664543956789895679878979989867892129895698
4598954569899964578999878987643499997689978789432129879998543212345678976989989459899756789298789789
3499543798798765699998767898984987843579899996583235989987632105468789987899992398789898999987678991
4985432987679876789999658989876986532498798889874346798798743236589893498989891987678989459986568990
9876521296567987899876545878989875421987656778965498987669654587679999999876789898547678998765456789
9876432987678998998765434569998767410996545567896569896556965689789987898865676789435589765432369999
3987543498799549769886325679987654329877434456789698789439879789899976987764545678923459879321287899
4599656789989432458997212568999769498764321298999987688956989897999899876743236789212398998932456998
9798767897678921367989323459998998999875210147899876567897891956789798765432145794301987897893569997
8999878996569432349876534569987687898654321236789767456789932345679659976545012689419876896789878986
7899989987458943499998765678976546789985434545678954345679543567789545987983234568998765645678989565
6789199654347896589989876789987434579876545758789543237789654579895434499874356789329874234567893434
5679298789756789678976987899874323459987859767899654345678965989965421298765667895499932125689932123
4568999898969898789765698998765212398498869878998765656789879898965432399876878976987891034567894034
3456789987898999899894329987654323987349978989999898767896999797896743987987899989896789123678985125
2346899876767892999989212398765499876567989999899959878934987676989659876798945698765695434679876789
1256998765458993498979903459876989987698999999789943989949898455678998765759899987654989545689989899
4349879876569989986567894967989878999789989897678892099898789334569987674545678998743478957897696999
5478968987678979765456999898998767879899876789546789298788699212989876543234567998932569768998565678
6568956998789569876567897789999857867998975695437995987657598909898995432123459886521678989895434567
7678939879892458987678956699896645456987764789567894696543467898767986321019598765432789496789323878
8789998765901456798789545598775435349876543999698943495432356789656997432198969876547892345689439989
9895987654312345789899434459654324234987875898789212989321234896549876545997656989856921234579998997
6954398767433566789998921398743210125698986789894309878540145789432997859876543398767890123467897686
5695999876544678998787892987654341234589987895999498765431236894320989767987432129898921294878987575
4989899987698789987656789398765492395678998934998989896549898965999878979876421012969939989999098464
2976789998789899876543789249876989989989239019887678987656789879878767898985432123457898679889198323
9895678999894968987654590123989879978990129198764569998987894998765459987899543434568987546778987634
8654547899923656798985891294598768969893298998765678999598912349821398795698976546699876435569876545
6543336789012349899876789989987656756789987899976789989499909496543989654567898687987665523456987676
7652125978923598987987899878996541234567896569899899978987898987859878965678998789876563212345698787
6543234567894987876798998769876532345978943456799998767896987598998969898799989898765432101234569898
7685346878999876765689987655989645567899212566778987658965398459987856789899978999876543212385789999
8876798989998765434569976743498756789956401234568998789876999349876546899988769899987654525678999999
9987899697989954323798765632359867899543212345679239899989899956997635789879656789999769434789898989
4599996546567893212987654321235978998764637566989139999998789899986523998967545698999898945699787678
3499989435457899433498765435348989689895547677891098988997655798765439897645634567899976896789676567
2989978921345678994569876546757894578987678788992987677893234569898698765430125698999865689896543456
9879868935458789989778987987868943989998989999789987566989123456989789876321234789997684578965432123
8965657899599999878989998999979659899879596545678976435778934569879894987432545679876543467896673294
7654545698989997569999999998989799789965432434789897324567895979964933498545789789987432456998784989
6543234987978965478999899987899987656974321023498788212379976798763212379676899899997544567899999878
5432129876568896567898789656789976549875432164589654323568987987654343456987895978999655778956798967
4321019987456789678987698943495987678987543765678965454569998998765454567898954567898776789345987954
5432998765345698989996587992976798789798765897789876875678999869876569878979943459979987893212986543
6549879876234767899985456789897899997659897899897987996789998754997878989767892598965398994323497632
7698765438123456789876323496789992198943998943956998987899987643298989997656999987890139989434598745
8899954321014567899865212345678989989894989012345899498989998759109499998747988976789239878965987656
9998765732125678999954345489789679878789876543456789349678939998912349876434567895678949867896798767
4349876653346789998769497679898998765699998754678991234569019887893498765325658934589998758659999878
4239999778659899899898989989967987854897899869789893965678998766799987654312349898699896549237899989
9398999889767998789987978995459876543786789878898789896799987655678998895401256789798765432126789997
8987899999879987678976567894398765432645699989997678789929876543767899986212868999899654321034599896
7856789432989876569875456894298764321237989899986565678910987432347678997343479756998789532123456789
6545678921098765454986587932129879854349876799867434568924596541234599987654569545689898743454567895
5436889932129854323697698943234988765698765987654523567895987762345789598968678934578999654765678954
4321968894298765634598789954445699878987654599763212348997898943579893459899789545678998765878789875
5872456789349878745679897895768789989877653459894103456789999874568932598789897676789129878989899986
8763878995467989856789976979879899998765432345989294567899899865679943987699998989899234989299999899
7654567896568995977891234568999978919876521239878989678956756978989894976568999195978976790199899788
8765698987878923988910123456789567923987432399765679789432347989998769876456789234567897892988698677
9878789498999219899321256587995456894898643989813478997643458994987656987567894345679998999876545556
2989893219879998765432347898932345995798759876524567899856969543499897897698987457989899298765434345
1099954523467899876545456999545476789899899976435788923987897652101998998789876567897654349854321237

5
2021/day09/test Normal file
View file

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

53
2021/day10/day10.scm Normal file
View file

@ -0,0 +1,53 @@
(import (chicken io)
(chicken sort)
srfi-1
matchable)
(define (read-input #!optional (port (current-input-port)))
(read-lines port))
(define (check str)
(let lp ((stack '())
(input (string->list str)))
(match (list stack input)
((() ()) #t)
(((#\( . srest) (#\) . irest)) (lp srest irest))
(((#\[ . srest) (#\] . irest)) (lp srest irest))
(((#\{ . srest) (#\} . irest)) (lp srest irest))
(((#\< . srest) (#\> . irest)) (lp srest irest))
((_ (#\( . irest)) (lp (cons #\( stack) irest))
((_ (#\[ . irest)) (lp (cons #\[ stack) irest))
((_ (#\{ . irest)) (lp (cons #\{ stack) irest))
((_ (#\< . irest)) (lp (cons #\< stack) irest))
((_ ()) stack)
((_ _) (car input)))))
(define (check-score c)
(match c
(#\) 3)
(#\] 57)
(#\} 1197)
(#\> 25137)
(_ 0)))
(define (part1 in)
(apply + (map check-score (map check in))))
(define (completion-value c)
(match c
(#\( 1)
(#\[ 2)
(#\{ 3)
(#\< 4)
(_ 0)))
(define (completion-score lst)
(foldl (lambda (acc c) (+ (* 5 acc) (completion-value c))) 0 lst))
(define (part2 in)
(let ((scores (map completion-score (filter list? (map check in)))))
(list-ref (sort scores <) (/ (sub1 (length scores)) 2))))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

94
2021/day10/input Normal file
View file

@ -0,0 +1,94 @@
<({<[(({(((({(<><>)[[][]]}<<()>{{}}>)(<{(){}}((){})>))<[((<><>){()()})[(<>{}){[]{}}]]>){{(({[]()}<[][]>
{<({[[<<{(<{(<{}()>([][])){<<>}[<>()]}}[<(<>()){()()}>[[(){}]({}())]]>{({{<>{}}[<><>]}<{()<>}{{}()
{{<<<[<({[{{<({}())(<>{})>(<()<>><()[]>)}<([<>{}](<>[]))<{{}<>]>>}<[[<<><>><[]()>][[[]{}]{{}}]][{(<>(
[[<<<[([[([<(({}[]){{}[]})><{([]<>)[(){}]}[{()[]}([]{})]>][({({}[])<[]{}>}{[{}()](<>())}){(([]{}){[][]})[
{((({{<[(((<(<()><()()>)>[{<[]()>{<>{}}}{([][])<[]()>}]){(({[][]}{()})(({}<>)[<>{}]))({{()<
[[[(({[<<[([[[[]<>]{[]()}]([{}()]{{}{}}]][{[()()]{(){}}}(([]())<{}>)])][{[(([][]){[]{}})]<{(()<
{<[{[({<{[{[<[()<>]>({[]<>}{[]()})]}{[([<>{}]({}{})){([]{})}]{{<{}{}>}{<()<>>(<>{})}}}](([{{(){}}<{}{}>
<{({{[(({({<[[[][]]]{[<>()][{}()]}>((<{}<>>))})(<(<[{}[]]({}<>)><({}<>)>)[[{()<>}{<>{}}}[{[]<>
[<[[[{<[([<(<<<>{}><(){}>>(<[]()>)){<<{}<>>[()]>[<[]<>>([][])]}><<{[{}()][{}[]]}<({}{})>>({{{}{}}}
{[[{<(<[{[<(<<{}{}>{[]()}>){{(()<>)[[]<>]}}>[[[[<><>]<[]{}>]]]]}({{{{([]<>)[[]{}]}}{{(()[])[[]{}]}}}}((<(<<
<[([({<({(<{[[[][]][<>()]][<{}[]>]}[{(()())<{}{}>}<({}())(<>{})>]>{(<<[]{}>>{{{}<>}[[]<>]}){{([]
{<<[<(<{[[<[{[()[]](<><>)}{<[]{}>(()[])}]<{{[]{}}<{}<>>}([[]()]([])))>(([[<><>]]))]<[<{{[][
(((<<([[([<<[(()[]]<()<>>]<(()[]){{}[]}>><<[{}{}](()())>{<()()>[{}()]}>>])(([[({(){}}<<><>>)[{{}[]}[{
[<{([[<(<<[[([<>[]][<>()])(((){})([]()))](<{[]<>}({}())>[<<>[]>(()[])])]{[((<>())[{}()])(([])((){})
(({<{<([[[(<(<[]()>{<><>}){[[]()]{{}<>}}>)]<([<{()[]}<<><>>>{{<>()}{[][]}}][<{[]<>}<{}()>>])[<{
<<<<[([({{(<[<<>{})]<[[][]][{}{}]>>[<{{}()}[[]<>]><[[]<>]{[]()}>])}}<(<([(()())({}())]({<>
{<[{{(<((<{<[<()()>[{}<>]][([]())[{}{}]]><{[[]{}]{()()}}{([]<>)<()<>>}>}><{(<<<>{}>[[]]><({}[])({}()]>)}<{[
(<{{<(([[({<(<{}><<>{}>)><{[(){}]}<{<>()}[[]<>]>>}{({<(){}}({}{})})})<<[[<(){}>{[]()}]<[{}{}]>]
<{((<{[{<{[((((){})(<>{}))[<{}[]>[()]])(<[()()]{()<>}>)]{{({()}<{}[]))}[<(()[])><[()[]]({}<>)>
(<(<({[{<{{{<[{}][[]<>]><{<><>}(<>[])>}}}>}<{<[({<[]{}>[{}()]}([()[]]{[]<>}))[[({}[])]{({}{})[{}[]]}]][<{{<>(
[({<<[{{{<(<<<{}{}>{[]}>({()[]}({}()))>{<[{}()][{}{}]>[[<>{}](<><>)]})>(<<[<{}<>>]{<[]{}>([
<(({{[(({(<{{([]<>)[{}<>]}}{<<[][]>{<>{}}>({<>()}{<>()})}>{[[[[]<>]](<<>{}><()<>>)]})({<([<
<<[(<<([([({<[{}[]]<{}()>]{<[][]>[[][]]}}({<()()>{()[]}}({<>()}((){})))){{(<()[]>[[]])(<<>{}>)}{<{{}[
{{(<<{([{[{(([{}{}]((){})))}{<[(()())({}())][{()<>}]>}][{<{({}())<[]()>}[[<><>]{<>()}]>}]}[((<
<[[{(([<<<{<([[][]][()[]}){{[]<>}(()[])}>([(<>[]){{}[]}]{({})<<>{}>})}[[<{<>[]}<[]()>>][{{()[]}}[{{}()
{{([{<((<{<[<<()[]>(<>())>{[<>()][[]<>]}]([({}[]][<>()]][(<>())<()<>>])>({<[[]<>]>[[()()][[][]]]}<[<[]()
[<<({<{[[({[<((){})[{}]><<[][]>{()[]}>][<({}{})>[[{}()]{{}<>}]]}{{<<[]()>([]())>{{<>()}{<>[]}}
[({[({{(<(<{<<<>[]><<>()>>({()[]}{()[]})]>){(([[()()](<>{})]{([][])<()()>})(({()[]}<{}()>)[
{<(<{<{(({{[(<<>()>{<>[]})(({}())[[]()])]([({}())(<>{}}])}{{<{{}()}[[][]]>{{[]{}}<<>[]>}}}}{<[{<
[((<{{{([<<<[({}[])[()<>]]<{<><>}<<>[]>>>>>[(<{[{}]<[]()>}{[<>]<[][]>}>{{(()[])}(([][]){<>[]}
{<((<{(({{{<([[][]]{{}{}}){{[]{}}{[]<>}}>[{<{}()>([]<>)}(<[][]>{<>[]})]}<([[()[]]]{{()()}([][])})([{[][]}<{}
[{([(([<{([(<{[]{}}[()[]]>(([])[<><>]))])[[{(({}())<[]<>>)<({}())[()[]]]}([<()()>[<><>]](<[]<>><<>()>))](
{{[[[(((([[{[([]())[()>]}(<<()<>>({}<>)>[{<>[]}[()<>]])]<<[{[]()}{[]{}}]>({[{}{}]([]{})}[{(){}}{{}[]}])>]
[({[<(<[{[{({<<>{}>{{}{}]}[(<>())[<>{}]])(((<>[])<{}[]>)[<()()>[{}{}]])}[[<<{}<>>{{}[]}>[(<><
[{<((([{{{(<{[{}<>][[]<>]}]){[({{}<>})<<{}()>{{}{}}>]{<([]<>)<[]{}>>[<(){}>(<>[])]}}}(([{[<>]<[]()>}([<>{}][
{(([<{[[<({[<<<>{})([]<>)>[{[]()}(<><>)]][<(()[])>{(<>{}){[][]}}]}<{[([]())<[]{}>]<{[]()}(()
<{[{[<<{[(([{(<>{})<[][]>}<([]<>)(<><>)>](<[<>[]][[]<>]>[[[][]]({}[])])))]{[[{({<>}[()<>])[<[]<>>{[]()}]}(
{(<(({{(<{<[<<<>{}>[<>[]]>([[]][[][]))]([(<>{})({})])><[[{{}[]}({}[])][{<><>}<{}>]](<([][]){[]}><{[][]
{[[(({{({<(<(({}<>){{}}){([])({}{})}>)>}<<[([{<>[]}[[]<>]])<({[]()}[{}<>])<[{}{}][[][]]>>][{<
({[{[{[<([{{[[()()](<>{})]((<>{})(()<>))}[({[]()}({}{}))[<<><>>{(){}}]]}<((([]){()<>}))>])><<<
[{<({([<(([{[{[]<>}][(()<>)<<>[]>]}]<(<({}{})(()())>[([][])[(){}]]){<<()[]>>}>))<[{<[[[]{}]([]())][{{}{}}
{[(((<<{[[(([<[]()>(<><>)])(<<<>{}>[<>{}]>{<<>><[]<>>}))[{(<<>()>[[]<>])(([]<>){<><>})}[((<>)[[][]])(([]{})[[
{([<<{((({[[[(<>{})[()()]]]]<[([{}{}]<[]<>>){<<>[]><()[]>}]>}{<<{<()()>}>{{([]<>)[[]<>]}{<()[]>[[
<<[(<(<[({<{<([]())(<>[])>[{{}<>}((){})]}>{({(<>())({}<>)})}}{<{<(<>[])><{[]{}}<{}()>>}[{(())(()[])}[({}())<[
{{([{{<{{<[(<[[][]]{{}()}>)[<{[][]}<{}[]>}({{}()}{()()})]][{[[[]()]{()[]}]<{{}}([]())>}{(<
[<<{{<<<[[[({[{}()]}{<{}[]>(()())}){({[]<>}([]{}))<({}{})(<>())>}]{({<[]<>>([]<>}}{{[][]}[[
(({[[[{<(<{[[([][]){<>[]}]([[]<>])]<{[{}()]<{}[]>}[({}<>)<{}()>]>}[[(<<>[]>[<>()]){<()<>>{{}{}}
{<{([[<<(<<{<[()<>][()()]>[[()[]]({})]}>>)>>]])<{({<<[[<([(){}]<[]()>)<<[][]>[<><>]>]]]>[({[{<()<>><
{({([(([({{((<()()>([][])))}{{{<<>>}[[()<>](()())]}<{<<><>><{}[]>}>}})({<[{<{}{}>[<>()]}[{{}<>}[{}{}]]]({(
{{(<{<[[(<(<[(<><>){[]()}]>)(([<{}{}>[[]<>]][<<><>>(<>{})])<<[[][]]><<[]()>{<>}>>)><<{<({}{})[<><>]>}<
{[[[[{{(<<{<{{[]{}}<{}[]>}[<()[]>[<>[]]]>{([{}()])(<{}{}>{<>{}}]}}[<<{<>{}}<()()>>{[<>]}>{[(<>{})<()<>>]
((<[{<[({<({[(()())(<>[])]<[<><>]{[][]}>}([([])[{}()]]<{<>()}{{}<>}>))><([<([]{}){[]}>[{[]}[[][]]]
[<[<({[{{({<[(<><>)<[][]>](<<>{}>(<>()))>}(<<<{}{}>[{}<>]><<[]{}>[<><>]>>(((<>())<()>))))}}[{[(<[{()[]
[{<[<(<<{[<([<{}[]><<>()>][[[]()]<()[]>])>[(<[(){}][{}{}]>{[<>{}]<{}()]})]]({[(<<>>)<[{}<>]<[]{}>>]<<<(){}><
[<{<{<<<(((([<()>{<><>}][(()())[{}()]])[[{{}()}<()()>][{()}]])<(<<[][]>><<{}()>[{}[]]>)[([[][]][<>[]]
({{<[{[{(<<[((<>[])<[]()>)[[{}{}]({}{})]]>([[(()<>)<<>{}>]{(()<>)[{}()]}]{(([]())([]()))[[[]<>][<>{}]]})>
({<<[<{{[[([{<()<>>}])[<{<()[]>}[{(){}]<{}[]>]>[[[[][]]](<[]<>>{<><>})]]]<[{[([]{})<()()>]}(<({}<>){<
<([<<{(<({{{<({})(<><>)>{{<>[]}<[][]>}}<([()[]]({}{}))>}})>{{({[[{{}<>}]{[(){}][(){}]}]([{{}{}}({}{})][[
{[({<[<[([[<<({}{}){()[]}>{([]<>)}>{(({}<>)<[][]>)[{{}<>}(<>{})]}]])[({[(([]<>){<><>})(([]<>)<[
<[[[{<<({<{([<()[]><[]()>])<[{{}()}<()[]>](([]<>){[]()})>}>{[[(<{}()){<>{}})[<[]()><()<>>]]{[{[][]
{[(<{<{{{[(((({}[]){(){}})(<(){}>{{}<>})){([[]<>]{[]<>})[[<>{}]{<>()}]}){(([<><>][{}[]])([<>]
[<({[[[[{<<{[{[]()}<[][]>](<{}<>>[{}{}])}{({{}()}((){}))([()<>]<<>()>)}>>[<([{()()}{[]()}]{[<
{<{{([{<([<[{<(){}>(()[])}<<(){}>{{}{}}>][{[(){}](<>())}[{<><>}<[]{})]]>({({[]()}[[][]])<{<>()}<[]()
[{{<[{<((([{{<[]{}>[[]{}]}{<<><>>[<>[]]}}](({<<>{}><[]>}<(<>)>)[((<>{}){<>[]})[{{}<>}<[]()>]]
([<<{([<[<<([([]{})[{}()]]<(<><>)<()<>>>)><([{{}<>}[(){}]]([{}<>](<>{}))){<<[]<>>{()<>}>[<()<>>(<>)
{{({({(<{({<{[()()]{[]{}}}[<{}[]>]>{[{<>[]}{{}()}](<<>{}>[{}<>])}}[{<[{}{}]<<>[])>([{}[]]{{}[]})
<{((<([<[{([([<>{}]([]<>))]{{<{}[]>[[][]]}})([(<{}{}>{()<>})[[[]()][{}{}]]}[{{{}()}<<>()>}
[{<[<<({({<[{<<>[]><<>{}>}<{<><>}<{}()>>][([[]()]{<>{}})]>{(<[{}[]]([]<>)>}<<[{}[]][{}<>]>{{{}()}{<>[]
[{<<([([[{<((({}<>)[{}()])[{[]()}(<><>)])<[<()<>)(<>())]>>([((<>()))[[{}]([][])]]<([<>()]<()()>)>)}(([<{
(<(([{<{(<([[[<>[]]{[][]}][((){})]])([((()())({}{}))(<<><>>)]<([[]<>]((){}>)({[]{}}{<>()})>)>)({{{{[
{<<{{(<{[<{{<({}[])<{}[]>><([]())<{}<>>>}<([[]{}]<()<>>)>}>{<(([<>[]]([]()))<{{}{}}(<>[])>){[([]()
{{(({<{[{[[[([{}<>](()))]{{{{}{}}{<>{}}}[[{}{}]([]<>)]}][([(<><>)[[][]]])<<(()())(()<>)>[([]()){()
(<[{<<[<<[(<{{<>{}}{[]<>}}[{[][]}[()[]]]>[<(<>())((){})>{({}{}){[]<>}}])]]>((([(<[<>{}]{{}<>}>{(
(<<{{<[{[(<<<[{}{}]([][])>[{()<>}[{}]]>{({()[]})(<()<>>(()))}>)}}][[<([({<{}>[<>[]]}){({()[]}(()[])){
<(<([<<({[{(((<>[])<<><>>){{<>{}>}){{[()[]]<{}{}>}{({}{}){<>()}}}}[(<<<>{}>[{}{}]>(<()[]>[<><>])
[{[<<{[<(([[({<>{}}(<>))<<()()>[<>{}]>][{{[]{}}}<<<>()>({})>]]><{([(<><>)]<<(){}>{{}{}}>){{{{}[]}<<><>>}[[()
[[[[<{(({{(<<<{}><[]{}>>((()[])[{}()])><<{[]<>}{<>[]}>[(<>()){{}<>}]>)(({(<>())({}[])}){[<(){}><[]{}>][[
<{[[<{<[<<[{([()()]{()()})[{[]()){{}{}}]}][{({<>{}})<[()()]{[]()}>}<[[(){}][{}[]]]{{<>[]}}>]><(([(()())
{<<(<(([<<{[{<[][]>}{([]())[()<>]}]{[[<>]<[][]>](([]{})(()[]))})[<<[{}<>]{<>{}}>[([]<>)[{}[]]]><(
[([({([([{<({<<>{}>[{}]})<[({}{})({}{})][{{}()}(<>{})]>><{{([]{})([][])}[[{}[]]<<><>>]}<((<><>)[()
([[[[{{{{{{{<<(){}>{<><>}}{[{}[]]({}[])}}<({{}[]}[<>]){<<>{}>{()[]}}>}{{{(()[])}{{{}}}}{[([]())
(<{(({<<<{(<[<[][]>]<<{}<>>{{}{}}>><[{[]<>][<>[]]][[<><>]<[]()>]>)[(<([]{}){[]{}}>({{}{}})
<((({[(<<{({(([][])){[[]<>]}}<(({}{})({}{}))(<[]()>[()()])>)}>({(<[([]())[[]<>>]([[]{}]<[]<>>)><{{()[]}
<{(<<({[(({{({<>[]}(<>{}))<<()[]>{<><>}>}<<<()[]>[(){}]>>})<{({(<>[]){<>[]}}[[<><>]])<[(())(<>[])
[([<[{[[{({{({[]<>}{[]()})([{}{}](()<>))}{(<[]<>><<>[]>)}})}<<([<[<>()]<[][]>>([[][]]{[]{}})](<<()
<<<(<({[{[{{(<{}>[{}{}])(([][])[(){}])}{([{}<>](<>))}}]}[<<{<((){}){[]{}}>({{}<>}{[][]})}<{{()()}<<><>>}>>{((
<<<{{{((<[<<{<[]{}>}><[({}{})[[]<>]]>>[<([<>()](()))<(<><>)(()())>>[{<()<>>{()()}}]]]({(({[]{}}({}[])))<({{
[<{[{[[{{{[{({<>[]}<[][]>)[{(){}}{{}()}]}<[[()()][{}()]](({}<>)[()()])>]<({<{}<>>({})}([()<>])){[{()<
<{{{{([(([{{{<{}[]>(()[])}{({}[])({}[])}}}])<{({<({}<>]<<>{}>>[<()<>>({}())]}[<<[]()>[<>()]>(<()<>>{<>[]})])
((([({[<[[([([<>[]]<[]<>>)(<()<>>)]{[{<>)[()()]][([]()){[]<>}]})]][{((<[<>[]]{<>()}>[<()<>>[{
<[<{[[<[<<{{(<[]{}>){[()]({}{})}}(<[()[]]{[]()}>[<()[]>])}{{({[]<>>([]{})){{()<>}{<>[]}}}(<(<
<[<<<[{<[<[[{(()<>)[()<>]}]({{()<>}({}())}[[{}()]])][{([<>{}]){({}[])([]{})}}]>[[[<(<>{})<(){}>>]}]]
<<[[[{[<[(<<<{<><>}(()[])>{{()()}[()[]]}>>)[<{[<[]()>({}{})]{{{}[]}[[]]}}{{{[]<>}{()()}}}>[[[[(){}]({}<>)]{
<[<((<<<{<[({[[][]]<{}{}>})({{<>{}}[()<>]}(({}{})[[]()]))]({{(<><>)<<>()>}{[[]{}]}}({(()())[{}()

10
2021/day10/test Normal file
View file

@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]

59
2021/day12/day12.scm Normal file
View file

@ -0,0 +1,59 @@
(import (chicken io)
srfi-1
srfi-42
srfi-152)
(define (read-input #!optional (port (current-input-port)))
(map (lambda (s) (string-split s "-")) (read-lines port)))
(define (big-cave? cave)
(char-upper-case? (string-ref cave 0)))
(define (small-cave? cave)
(char-lower-case? (string-ref cave 0)))
(define (neighbours edges vertex)
(let lp ((acc '())
(edges edges))
(cond
((null? edges) acc)
((equal? vertex (car (car edges))) (lp (cons (cadr (car edges)) acc) (cdr edges)))
((equal? vertex (cadr (car edges))) (lp (cons (car (car edges)) acc) (cdr edges)))
(else (lp acc (cdr edges))))))
(define (find-all-paths edges start end)
(define paths '())
(let lp ((path (list start)))
(if (equal? (car path) end)
(set! paths (cons (reverse path) paths))
(do-ec (: n (neighbours edges (car path)))
(or (big-cave? n)
(not (member n path)))
(lp (cons n path)))))
paths)
(define (part1 edges)
(length (find-all-paths edges "start" "end")))
(define (small-caves-at-most-once-except-one? path)
(let ((small-caves (filter small-cave? path)))
(<= (- (length small-caves) (length (delete-duplicates small-caves))) 1)))
(define (find-all-paths-2 edges start end)
(define paths '())
(let lp ((path (list start)))
(if (equal? (car path) end)
(set! paths (cons (reverse path) paths))
(do-ec (: n (neighbours edges (car path)))
(and (not (equal? n "start"))
(or (big-cave? n)
(small-caves-at-most-once-except-one? path)))
(lp (cons n path)))))
paths)
(define (part2 edges)
(length (find-all-paths-2 edges "start" "end")))
(let ((edges (read-input)))
(print (part1 edges))
(print (part2 edges)))

23
2021/day12/input Normal file
View file

@ -0,0 +1,23 @@
dr-of
start-KT
yj-sk
start-gb
of-start
IJ-end
VT-sk
end-sk
VT-km
KT-end
IJ-of
dr-IJ
yj-IJ
KT-yj
gb-VT
dr-yj
VT-of
PZ-dr
KT-of
KT-gb
of-gb
dr-sk
dr-VT

7
2021/day12/test1 Normal file
View file

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

10
2021/day12/test2 Normal file
View file

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

18
2021/day12/test3 Normal file
View file

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

52
2021/day13/day13.scm Normal file
View file

@ -0,0 +1,52 @@
(import (chicken io)
srfi-1
srfi-42
srfi-113
srfi-128
srfi-152
matchable)
(define-record point x y)
(define (read-dot s)
(map string->number (string-split s ",")))
(define (read-fold s)
(let ((n (string->number (string-drop s 13))))
(match (string-ref s 11)
(#\x (lambda (x y) (if (< x n) (list x y) (list (+ n (- n x)) y))))
(#\y (lambda (x y) (if (< y n) (list x y) (list x (+ n (- n y))))))
(_ (error "invalid fold axis")))))
(define (read-input #!optional (port (current-input-port)))
(let ((in (read-lines port))
(string-not-null? (lambda (s) (not (string-null? s)))))
(values
(map read-dot (take-while string-not-null? in))
(map read-fold (cdr (drop-while string-not-null? in))))))
(define (apply-fold fold-fn dots)
(set->list (list->set (make-default-comparator) (map (lambda (l) (apply fold-fn l)) dots))))
(define (part1 dots folds)
(length (apply-fold (car folds) dots)))
(define (part2 dots folds)
(if (null? folds)
dots
(part2 (apply-fold (car folds) dots) (cdr folds))))
(define (print-dots dots)
(let ((xmax (+ 1 (apply max (map car dots))))
(ymax (+ 1 (apply max (map cadr dots)))))
(do-ec (:range y ymax)
(begin
(do-ec (:range x xmax)
(if (member (list x y) dots)
(display "#")
(display ".")))
(display "\n")))))
(let-values (((dots folds) (read-input)))
(print (part1 dots folds))
(print-dots (part2 dots folds)))

941
2021/day13/input Normal file
View file

@ -0,0 +1,941 @@
92,632
795,9
1034,368
1230,233
969,816
1218,526
971,780
139,417
892,313
1196,196
1091,70
728,747
632,10
1089,247
70,159
492,375
919,581
304,469
736,431
7,630
457,879
174,567
328,773
207,714
361,452
77,151
907,298
325,838
956,644
272,52
259,794
254,847
517,851
669,516
746,275
1146,801
1019,297
679,43
517,416
703,814
90,201
206,74
239,794
1120,114
197,711
1247,131
239,71
418,805
129,757
1195,56
895,598
975,712
57,171
656,746
984,177
1,182
785,457
810,107
703,278
699,593
795,477
67,750
190,616
1,712
162,438
1079,432
1010,868
431,30
293,9
957,518
452,634
1245,452
631,87
395,200
721,284
1143,198
669,378
1205,827
974,483
33,588
894,7
1064,245
1099,616
1307,248
63,234
249,71
919,182
589,284
730,152
251,298
644,78
731,148
669,638
663,712
356,824
960,873
1139,602
902,404
785,728
830,434
691,207
118,567
927,742
436,556
946,866
1235,746
433,647
1266,103
267,775
796,581
474,219
686,582
982,354
897,263
398,626
689,687
1184,294
403,374
303,135
1168,670
1275,562
1000,812
974,411
785,309
1019,108
189,551
969,857
401,549
574,873
73,885
417,406
1029,584
345,626
238,887
103,847
1006,21
919,712
256,268
13,773
1093,644
1230,890
858,260
1010,196
1002,341
761,807
1034,526
661,318
381,390
1111,597
1161,65
1258,152
1031,56
393,890
683,752
22,621
671,49
972,31
581,574
1163,145
1051,694
805,513
1119,28
1051,794
1288,884
1092,226
152,270
174,327
266,151
763,863
733,151
1292,105
55,47
663,630
190,780
1170,537
177,381
550,390
251,150
23,43
1007,135
386,726
431,847
1079,24
171,574
997,212
1056,89
1238,257
74,775
152,119
344,107
833,793
1146,737
972,150
107,600
505,605
1124,273
474,194
17,380
492,823
1309,712
719,131
830,460
1237,574
1180,28
808,176
507,98
339,114
155,261
318,859
335,824
919,548
920,497
1074,413
1221,233
118,103
1079,14
673,518
761,197
289,96
602,294
1064,218
549,197
1091,600
375,80
691,625
965,268
574,469
890,191
796,89
547,415
3,248
858,95
162,71
748,526
222,392
994,526
803,796
868,565
218,42
293,718
763,31
1222,742
279,56
966,107
74,791
455,177
507,796
1071,71
875,518
499,875
177,45
1258,70
659,248
467,750
1044,151
244,714
1260,480
576,476
212,26
181,156
1071,346
239,598
893,406
666,658
1086,401
114,565
171,633
103,495
290,476
1098,420
1268,148
582,257
408,452
310,787
435,376
991,824
1130,518
333,514
1148,413
90,649
649,457
326,298
1155,875
607,814
868,329
1302,859
1048,532
259,884
1006,469
1155,712
587,122
868,250
1155,261
328,705
604,414
271,175
974,247
621,687
1054,211
298,70
647,793
857,432
293,207
190,838
842,173
238,439
113,383
938,530
11,114
1074,295
736,661
1193,666
364,642
525,728
405,590
1089,199
199,877
1206,726
835,301
621,400
1111,297
1238,637
580,551
969,37
945,768
189,343
164,737
890,703
661,514
820,317
13,170
219,175
551,847
512,858
1031,814
1031,260
637,376
1130,667
105,255
1097,735
1305,828
619,625
1126,367
1087,30
13,121
47,726
416,679
338,31
647,630
313,343
729,320
82,571
1139,574
465,491
1017,207
234,847
137,478
875,527
956,26
591,660
514,581
326,189
1303,152
483,857
869,831
483,37
739,863
383,600
502,723
919,96
169,437
33,157
7,712
1250,665
1098,644
792,280
236,413
813,437
107,863
1001,474
1001,626
1092,194
666,638
202,95
223,210
907,596
380,600
1087,847
475,301
114,196
977,206
666,82
119,266
716,823
689,437
303,9
10,658
371,637
773,241
1000,586
935,56
1202,220
699,628
518,280
1076,210
273,838
259,880
972,66
798,450
236,581
763,600
816,227
1250,229
2,250
1120,126
1002,789
731,772
222,56
1057,345
268,726
802,425
335,182
364,252
949,885
464,780
344,630
1130,443
1049,162
206,820
1168,858
982,298
10,275
363,786
1161,605
42,298
1158,492
782,742
171,709
350,682
1240,159
985,504
734,418
364,866
341,485
1203,751
902,732
80,213
338,150
55,847
726,235
536,775
647,661
621,588
445,666
1235,74
954,824
610,791
1000,406
949,562
82,91
1133,45
436,42
1285,638
631,291
416,663
1017,114
874,194
736,233
441,831
902,452
480,434
949,780
272,500
666,12
1099,479
57,311
130,866
631,43
925,499
868,278
42,148
1266,551
657,630
300,226
118,327
1089,647
47,504
1184,742
966,140
556,700
1088,392
239,548
146,376
895,151
549,697
897,631
644,414
657,182
641,638
146,667
652,644
811,709
88,152
1300,598
336,411
1054,716
105,827
1309,630
304,21
1287,851
736,540
883,182
734,194
201,462
1228,120
783,381
1230,213
574,233
1017,885
528,742
905,304
774,551
343,621
836,219
420,703
748,859
500,18
647,600
415,548
300,26
1196,747
152,402
734,866
965,514
364,194
154,787
7,518
25,638
802,441
972,828
171,320
383,495
457,687
355,812
17,868
212,868
1181,885
190,56
793,739
263,182
181,738
1303,712
505,513
565,406
1205,67
508,425
982,637
75,74
795,190
1236,152
1164,376
915,78
293,687
36,630
80,354
1019,597
537,534
1202,642
310,308
92,526
437,644
836,194
155,409
413,631
1193,86
7,264
1017,306
1120,768
1011,304
966,586
728,537
527,65
1193,205
1173,2
1146,295
63,682
1287,535
1253,311
551,47
652,698
443,479
63,548
584,683
256,211
798,892
1268,298
691,114
1220,201
1091,724
1201,868
246,666
1148,800
78,642
1043,775
1161,180
1091,518
1027,628
649,128
1069,183
164,157
417,488
679,87
223,864
1300,236
1130,70
492,325
267,152
935,814
766,182
80,890
219,70
436,338
949,452
939,705
664,859
180,21
517,43
947,513
1146,463
764,252
811,427
1133,849
303,134
736,354
82,452
1007,759
164,352
1017,9
611,628
1238,17
44,103
892,581
1143,646
152,624
846,838
1139,320
890,236
351,424
845,674
1310,11
551,399
1228,263
326,717
1308,698
1072,439
679,291
754,700
331,198
731,820
982,596
80,233
1136,684
1277,306
13,704
1280,582
103,460
647,101
1124,425
319,824
733,375
654,33
1283,306
783,65
201,432
574,4
1038,394
1310,861
938,364
95,656
997,551
200,376
313,78
564,201
902,325
164,381
1201,646
678,10
231,813
361,9
977,514
117,86
646,859
427,182
984,637
52,70
560,142
1101,666
574,764
1143,696
246,228
823,646
127,269
164,463
773,808
408,442
1230,354
151,444
1148,438
146,443
239,738
256,716
656,861
290,264
303,457
249,823
353,518
540,196
338,66
1288,234
482,413
331,696
759,460
197,191
114,147
780,410
811,633
1007,155
313,485
1120,56
1087,658
129,137
345,268
846,278
300,698
508,3
847,28
822,667
487,646
244,442
574,540
689,530
211,616
78,252
159,646
894,663
1098,868
730,551
436,700
1031,437
946,488
11,780
366,858
452,351
73,9
1192,103
1021,798
508,586
1235,148
1232,642
5,437
547,143
663,569
1299,114
10,816
1300,812
684,824
745,406
503,674
1247,96
744,742
115,838
621,207
1183,65
1299,668
474,667
1292,519
361,754
545,248
974,859
579,820
117,205
512,892
509,518
808,12
8,859
1274,264
145,152
836,70
836,451
761,639
1104,820
610,103
811,37
82,442
793,851
241,183
180,451
1062,700
212,278
592,852
1087,864
418,357
743,828
527,829
199,597
483,602
490,705
743,598
80,793
517,739
611,593
1156,787
765,644
864,432
1146,431
763,415
581,885
654,11
1148,879
1044,677
499,467
746,299
180,443
1133,101
33,400
381,196
774,775
847,476
162,456
145,44
171,516
442,250
995,114
1058,600
957,70
43,266
792,320
1297,773
721,452
246,676
649,318
180,667
582,357
892,357
160,219
304,660
746,270
408,121
719,630
579,263
60,677
720,329
431,47
344,219
231,880
299,530
579,122
293,158
1287,43
564,59
845,491
820,766
433,247
1059,744
644,236
949,392
335,712
802,675
734,252
442,565
770,565
171,602
1048,362
502,775
1303,264
1051,365
1293,26
853,879
333,688
644,816
136,574
719,234
392,362
774,119
1087,684
699,266
736,764
1133,381
371,257
793,43
1191,266
341,346
607,373
147,145
905,590
102,849
505,381
1113,703
748,816
782,152
197,183
431,684
139,29
746,624
1247,630
1300,12
1155,19
631,359
72,877
1083,467
382,170
298,824
730,600
946,194
162,15
512,450
1011,28
423,141
488,667
1236,775
793,70
2,644
919,122
856,518
728,257
723,325
833,773
211,415
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

21
2021/day13/test Normal file
View file

@ -0,0 +1,21 @@
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5

58
2021/day14/day14.scm Normal file
View file

@ -0,0 +1,58 @@
(import (chicken io)
srfi-69
srfi-152
matchable)
(define (parse-polymer str)
(let ((str-lst (string->list str)))
(map (lambda (c) (string->symbol (string c))) str-lst)))
(define (parse-rule str)
(apply cons (apply cons (map parse-polymer (string-split str " -> ")))))
(define (read-input #!optional (port (current-input-port)))
(let ((lines (read-lines port)))
(values (parse-polymer (car lines))
(alist->hash-table (map parse-rule (cdr (cdr lines)))))))
(define (template->pairs template)
(define h (make-hash-table #:initial 0))
(let lp ((polymer template))
(match polymer
((a b . rest) (hash-table-update! h (list a b) add1) (lp (cons b rest)))
((a) h)
(() h))))
(define (template->counts template)
(define h (make-hash-table #:initial 0))
(for-each (lambda (c) (hash-table-update! h c add1)) template)
h)
(define (polymer-step pairs counts rules)
(define new-pairs (make-hash-table #:initial 0))
(define new-counts (hash-table-copy counts))
(hash-table-for-each
pairs
(lambda (pair v)
(let ((elt (hash-table-ref/default rules pair #f)))
(if elt
(begin (hash-table-update! new-pairs (list (car pair) elt) (lambda (x) (+ x v)))
(hash-table-update! new-pairs (list elt (cadr pair)) (lambda (x) (+ x v)))
(hash-table-update! new-counts elt (lambda (x) (+ x v))))
(hash-table-update! new-pairs pair add1)))))
(values new-pairs new-counts))
(define (update-polymer pairs counts rules steps)
(if (= steps 0)
(values pairs counts)
(let-values (((new-pairs new-counts) (polymer-step pairs counts rules)))
(update-polymer new-pairs new-counts rules (- steps 1)))))
(define (part12 template rules steps)
(define-values (pairs counts) (update-polymer (template->pairs template) (template->counts template) rules steps))
(let ((vals (hash-table-values counts)))
(- (apply max vals) (apply min vals))))
(let-values (((template rules) (read-input)))
(print (part12 template rules 10))
(print (part12 template rules 40)))

102
2021/day14/input Normal file
View file

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

18
2021/day14/test Normal file
View file

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

72
2021/day15/day15.scm Normal file
View file

@ -0,0 +1,72 @@
(import (chicken io)
srfi-1
srfi-42
srfi-113
srfi-128
srfi-152
matchable
generalized-arrays
storage-classes)
(define (string->list-of-ints s)
(map (lambda (x) (- x 48)) (map char->integer (string->list s))))
(define (read-input #!optional (port (current-input-port)))
(nested-list->array
(map string->list-of-ints (read-lines port))
u8vector-storage-class
2))
(define (neighbours grid p)
(match-let ((#(m n) (array-shape grid))
(#(x y) p))
(list-ec (: i '(-1 0 1))
(: j '(-1 0 1))
(and (not (= i j 0))
(or (= i 0) (= j 0))
(>= (+ x i) 0)
(>= (+ y j) 0)
(< (+ x i) m)
(< (+ y j) n))
(vector (+ x i) (+ y j)))))
(define (min-unvisited-index array index-list)
(let ((index-list (set->list index-list)))
(fold (lambda (v acc) (if (< (array-ref array v) (array-ref array acc)) v acc))
(car index-list) index-list)))
(define (part1 grid)
(define-values (m n) (match (array-shape grid)
(#(m n) (values m n))))
(define unvisited (list->set (make-default-comparator)
(list-ec (: i m) (: j n) (vector i j))))
(define frontier (set (make-default-comparator) #(0 0)))
(define dist (make-array vector-storage-class (array-shape grid) (* 10 m n)))
(array-set! dist #(0 0) 0)
(let lp ((v #(0 0)))
(do-ec (: n (neighbours grid v))
(if (set-contains? unvisited n))
(begin
(array-set! dist n (min (array-ref dist n) (+ (array-ref dist v) (array-ref grid n))))
(set-adjoin! frontier n)))
(set-delete! unvisited v)
(set-delete! frontier v)
(if (set-contains? unvisited (vector (sub1 m) (sub1 n)))
(lp (min-unvisited-index dist frontier))
(array-ref dist (vector (sub1 m) (sub1 n))))))
(define (expand-grid grid)
(match-let ((#(m n) (array-shape grid)))
(define new-grid (make-array vector-storage-class (vector (* 5 m) (* 5 n))))
(do-ec (: k 5) (: l 5)
(: i m) (: j n)
(:let new-val (add1 (remainder (+ k l (sub1 (array-ref grid (vector i j)))) 9)))
(array-set! new-grid (vector (+ i (* k m)) (+ j (* l n))) new-val))
new-grid))
(define (part2 grid)
(part1 (expand-grid grid)))
(let ((grid (read-input)))
(print (part1 grid))
(print (part2 grid)))

100
2021/day15/input Normal file
View file

@ -0,0 +1,100 @@
4552285989441124719798465825773318252269422625731628851155123123687151412171224298271625249662328175
8311826917677295561395716245426184212471311214851351547842124165674922644719934113217134531111711843
3134155528231296313292951689371671611355199429313261226392991129959293212461289411393263181213122398
7134159681533331172991932819128718131121428715191238612623891233112534231482681182354129791911339131
3291622121893821258215111531614233999661649271811871529461211115739821388973611183517651567314846349
9116197319992223784973749159912918948514855218957329971416177152174169497812373219984133121155319113
4968439114135996923939621521227438616432262962572553451333141591641121311129913812892293179114231721
6239219785234675391281641114148225942622192542113712327361223716951191275736388845623994891799592928
1939273152114165815621898131741625142273757411141673112259897358299222269191191214321758245326199814
3816295121211713919918129141521748315579413854273125311823434671365133636981112367451294972238794443
2852174392175217891292769297329734141529288318181221953212137313858911222727424134319782324839616229
8183411981136718428823127681411246528222138421871778251231423171588642328395114123811125612118626218
3338418958818229331399513691921921869922413136194421828291197132791812222241633461426938621462118999
6118582765375541144491519871123121172943678873247541921192915151717957812714841145223161116244141719
1469257418313218951211511591224281868433285518896571868973996776391916421179863524342144166273783912
1422118419319515496189391158516462277393622129936871982217562457129964219912124123218312977313682811
8162546139952136189911177533357821252891975965218152184112132367245486747212214333324221119951528114
1488239611461491973865185242959577752914191817477523179143671148316542517213281111151119111864959392
2218917661868133141243819962413216131979913232799988871913939111823317421712824994542992792255223561
5734326732884792428191241121191811111111473387959653938269214112192296162111121271889621211118195111
1511935111861121935741768338923128124661819191925137121591321229121111755219811212182121742778172224
9691562183513388221726493821238419121411141652415724257169175511984419826317912119999434678121339231
2729237532959444733225435991119461921916882435586112311223124141186312488218129149812515658171863425
7312559181323743938221212236191514318418548186139317233736171333421416212237192119355192471989892212
9415747738346141691418825197173192515219266172136455452815162517475883863143239112116141841148451749
1716263121115631141784252251314588413213814511691451393911924811572861144235991292175255372133811315
2113414911731164962342223421317921647461491268117386815433911112541511131114761959253123128112819312
5611912334927221252711276871211624911564162161151291141471494555221624112246614151591111923265776291
1232313271113554189955413155112699163114821911353171341215485252647889646619236253688482351315735369
2928123121191267721323822133384328412616119246178131321245693192617151348489271993468574213234923161
3738152544625516916693642738573193818379635241112299568611511432174519855133859227139712691931163454
4978932237312615451115123994194181387239461985218454596664714694169812591251542128791112588238428113
3912899182798916628918131979371112226953512814111311128381119548221199616478868331875333186481121175
1192927421372584232141915111886499529492278636769145793985139259812374369331982931471421336131151625
3911185215582434844193161925151121286721292842811119362211484112143921956211724157122539837492394492
2953216722815212632732453117127324334249682233371211614762722313186947295795983392311121826197161624
1198382767218312279682278191822374559293793873174499211516436372431371961648282146283935293224883912
2721884412949147613342941184192811799669771915122149974294638981641377324238731539914369681342394477
3115118226257112172214531484191979121212629611941829729475142214919216183112798119132131299929929699
1466138152589912253129981816414331451718321345795981111891281781111972184919821925923141344127119916
9122134452846226111158128491291121145921222611191948931811123751226221716719166843766823615463271932
6619413231595241518519791341359886562981116111712917233731162191991239432652386931159591326187113817
1591845559162771847321152825187221391211122541281244754119292979548984493162991919959714934322337916
9145672294212142439213811931976372247362114829339211131128281865174113912121498492324131148121412341
1756381967913432222112226193132111121163731192131143782119299311666751422511193161912311177255311491
7115421916752266197811918146237953712817131391633913821814582931412513381932142138371125246189914126
1598759376392514417822195284118133472211872183424131361292781113442751745432173337612973642181819911
1591927514986431542931331139613924212515831921359261755424117232619116257358732811361551433133715429
2212111786268518388677923211533188345939141431423322741185388818746937933316139596963917115156513628
1855546869784857736214551457262769215529832121261231613433399962496153414792119714419153328447319921
3511415871713341517259565232116433981351421678135321191415221837295414292764788231114826312162341834
1318126219119423358223393722114232919911513119438148878413538911488323992254252914857272139929315563
4289226872419291861111383538111219112723121131591971688293116191243172327912122531151228931163825361
4267158139198923577615628157767215331521989227229273443329721698929373269121122111211118234885216413
1721281392353248918171111348431632192885983433192911673143212314835824251437392163131172671133214211
5692434767167216246181911647487628151199631629919256742261113166114149911986911217644914329391511712
3171778979133384821415911915931112614411161443519188221518117991151822193121471212118516171612194481
5728892915394116535685132841521314914121155636512129949659431291134139338413127321115791617158219492
5922861319161211595287232112219437811222352666151929995431111444364119321358256314981447283261239184
3222922881213813538112221219919839222562138222214373588415721997345273115221926221171788323425398221
7121892289471757714164192317199889321525289161191684627127231622121813228164928171711883131411171115
4142133195631891911149439213662211433837321296381465816173997759492225942284821492317392226316171421
7171767952218516859111921382981412117845155531414929285844619281338151913746922229418521329595329132
4816434489225615129112978111511319222163357872511891321872136916132598114322114834241711175313191619
2514669152191264429122527554915621525232982214127922591124617421227986192172464521171554113293241255
1774416897439364241311525518963326121825461491834135917315678145446394222119252415181557212319979836
2924418171212166376217332219928524125161151381352283981249219536811189593191191599828153912124771153
4727284757417238221195571727143816932519912332164191111313562582931769391773963555132631269421642112
1826176132932591211811281494184124649671144935551536222129675132732112314333131883173928216124343136
3941791891125279118465128381591141714936412111144872142196359856953125116815582139618126238119115194
6384117152771514979552471442881941755398722446886791413239114561671931482126123895183722933123653386
8288171182979883467251168211845926325328548779112522134122556292344839656392433126114296546576969111
2138989191121173198114315232525383591485375339363419514352517573221121171933413254921418513742631831
2213937521219111556424717112813918859811613149217542121258771325685446299113436243924329111883434559
5617411212729914494182219773316932281919155593277131423859448626521722117873728694549823541912312231
7221118972111421391134729292837321281576312986563112977458467319418639282574649243117271379499867916
7481381681214113329839118518993911987199743449929119497141229931317315347953958761558423818144714914
1962828621421912127195245938424121754697118642194232414827848912318131722969611192541614712413124389
1453414916661118796193123189169627934943195131587127135111441988921215121971461323995311761192254778
1518323154639911222886514111893851359235192392211115741135984919811553443945191524421112271149351165
2124619118928812488832412319378222622441927332925587491814133367139228519219159512159781839661391252
9136123238713391213615338836814173613372192337512891881479195988521936915119119435812284115134985121
3187261985313118116972571821212913911963714631126731221266314529196541119322571137312995744923631544
2332196771181422121471611133259321313136947141217624113313181189182899571613229611511529134392846341
2251119425212277151215291781189314975221719413134967665311869114521729499121116213885163746111535137
9586226199417317151915282291149995631781731851159711348611212628493271263152555273213316776492793658
9912626841129251219151217833912973311223772814496593297471731359818991699147989928243894313247532231
3418241618947516332227613831374921874855584125558114429471121321232177214321598521597161191128216954
8822616123274861411769128213421232113175792178713153351744856521396128117147182712443411512198547621
5136333391414812842139999627612251181119116134115993237221194432774327464926135181519819157243157311
3251712384221229339111197239365114189176819952384125831111975148571184652387121257481133311981238514
4179221897253196943111444994381452998232779759947119714529137541964411154341851412468966612128546572
3561511861976915288819136237471569199991221811128611595299121117942329621124911194511484259317211339
7111129229731261111283415393296113773364287191681115113218825813213487828249222962695115515691873514
7329411182343148337133461144181931219118279336638688643366271821255171224147641198181918236522367641
9283273114241197532168149326977442972132134212116845582279681563772677122931571239535226581819843529
1998826211243465142314171494115638947312397589362193711385179619216332311195192919797766423111769171
5525112294591818267911389162211123228611241372111487571111114322132741612141213853137289112282145369
3749112349379788119372851572694972255221141172838213557111196117255932787266739885219667115595878115
2711353211472121989372124182678117398917811239219156315953193628344552163451161917419183114199897172

10
2021/day15/test Normal file
View file

@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

127
2021/day16/day16.scm Normal file
View file

@ -0,0 +1,127 @@
(import (chicken io)
(chicken format)
srfi-1
matchable)
(define (read-input #!optional (port (current-input-port)))
(read-line port))
(define (hex-char->bin hex-char)
(match hex-char
(#\0 '(0 0 0 0))
(#\1 '(0 0 0 1))
(#\2 '(0 0 1 0))
(#\3 '(0 0 1 1))
(#\4 '(0 1 0 0))
(#\5 '(0 1 0 1))
(#\6 '(0 1 1 0))
(#\7 '(0 1 1 1))
(#\8 '(1 0 0 0))
(#\9 '(1 0 0 1))
(#\A '(1 0 1 0))
(#\B '(1 0 1 1))
(#\C '(1 1 0 0))
(#\D '(1 1 0 1))
(#\E '(1 1 1 0))
(#\F '(1 1 1 1))))
(define (hex->bin hex)
(concatenate (map hex-char->bin (string->list hex))))
(define (bin->number bin)
(let lp ((acc 0)
(l bin))
(if (null? l)
acc
(lp (+ (* acc 2) (car l)) (cdr l)))))
(define-record val
version num)
(set! (record-printer val)
(lambda (x out)
(fprintf out "#,(val (ver ~S) ~S)" (val-version x) (val-num x))))
(define-record op
version type args)
(set! (record-printer op)
(lambda (x out)
(fprintf out "#,(op (ver ~S) (type ~S) ~S)" (op-version x) (op-type x) (op-args x))))
(define (decode-version raw)
(values (bin->number (take raw 3))
(drop raw 3)))
(define (decode-type-id raw)
(values (bin->number (take raw 3))
(drop raw 3)))
(define (decode-num raw bits)
(let ((new-bits (take (cdr raw) 4)))
(if (= 1 (car raw))
(decode-num (drop raw 5) (append bits new-bits))
(values (bin->number (append bits new-bits)) (drop raw 5)))))
(define (decode-op version type-id raw)
(if (zero? (car raw))
(let* ((subpackets-length (bin->number (take (cdr raw) 15)))
(subpackets-raw (take (drop raw 16) subpackets-length)))
(let-values (((subpackets _) (decode-all-packets subpackets-raw)))
(values (make-op version type-id subpackets) (drop raw (+ 16 subpackets-length)))))
(let ((subpackets-count (bin->number (take (cdr raw) 11))))
(let lp ((i subpackets-count)
(packets '())
(raw (drop raw 12)))
(if (zero? i)
(values (make-op version type-id (reverse packets)) raw)
(let-values (((packet new-raw) (decode-packet raw)))
(lp (sub1 i) (cons packet packets) new-raw)))))))
(define (decode-packet raw)
(let*-values (((version raw) (decode-version raw))
((type-id raw) (decode-type-id raw)))
(if (= type-id 4)
(let-values (((num raw) (decode-num raw '())))
(values (make-val version num) raw))
(let-values (((op new-raw) (decode-op version type-id raw)))
(values op new-raw)))))
(define (decode-all-packets raw)
(let lp ((packets '())
(raw raw))
(if (every zero? raw)
(values (reverse packets) raw)
(let-values (((packet new-raw) (decode-packet raw)))
(lp (cons packet packets) new-raw)))))
(define (version-sum packets)
(let lp ((versions '())
(packets packets))
(match packets
(() (apply + versions))
((($ val ver _) . rest)
(lp (cons ver versions) rest))
((($ op ver _ args) . rest)
(lp (cons ver versions) (append args rest))))))
(define (part1 hex-str)
(version-sum (decode-all-packets (hex->bin hex-str))))
(define (eval-packet packet)
(match packet
(($ val _ num) num)
(($ op _ 0 args) (apply + (map eval-packet args)))
(($ op _ 1 args) (apply * (map eval-packet args)))
(($ op _ 2 args) (apply min (map eval-packet args)))
(($ op _ 3 args) (apply max (map eval-packet args)))
(($ op _ 5 args) (if (apply > (map eval-packet args)) 1 0))
(($ op _ 6 args) (if (apply < (map eval-packet args)) 1 0))
(($ op _ 7 args) (if (apply = (map eval-packet args)) 1 0))))
(define (part2 hex-str)
(eval-packet (decode-packet (hex->bin hex-str))))
(let ((hex (read-input)))
(print (part1 hex))
(print (part2 hex)))

1
2021/day16/input Normal file
View file

@ -0,0 +1 @@
C20D59802D2B0B6713C6B4D1600ACE7E3C179BFE391E546CC017F004A4F513C9D973A1B2F32C3004E6F9546D005840188C51DA298803F1863C42160068E5E37759BC4908C0109E76B00425E2C530DE40233CA9DE8022200EC618B10DC001098EF0A63910010D3843350C6D9A252805D2D7D7BAE1257FD95A6E928214B66DBE691E0E9005F7C00BC4BD22D733B0399979DA7E34A6850802809A1F9C4A947B91579C063005B001CF95B77504896A884F73D7EBB900641400E7CDFD56573E941E67EABC600B4C014C829802D400BCC9FA3A339B1C9A671005E35477200A0A551E8015591F93C8FC9E4D188018692429B0F930630070401B8A90663100021313E1C47900042A2B46C840600A580213681368726DEA008CEDAD8DD5A6181801460070801CE0068014602005A011ECA0069801C200718010C0302300AA2C02538007E2C01A100052AC00F210026AC0041492F4ADEFEF7337AAF2003AB360B23B3398F009005113B25FD004E5A32369C068C72B0C8AA804F0AE7E36519F6296D76509DE70D8C2801134F84015560034931C8044C7201F02A2A180258010D4D4E347D92AF6B35B93E6B9D7D0013B4C01D8611960E9803F0FA2145320043608C4284C4016CE802F2988D8725311B0D443700AA7A9A399EFD33CD5082484272BC9E67C984CF639A4D600BDE79EA462B5372871166AB33E001682557E5B74A0C49E25AACE76D074E7C5A6FD5CE697DC195C01993DCFC1D2A032BAA5C84C012B004C001098FD1FE2D00021B0821A45397350007F66F021291E8E4B89C118FE40180F802935CC12CD730492D5E2B180250F7401791B18CCFBBCD818007CB08A664C7373CEEF9FD05A73B98D7892402405802E000854788B91BC0010A861092124C2198023C0198880371222FC3E100662B45B8DB236C0F080172DD1C300820BCD1F4C24C8AAB0015F33D280

59
2021/day17/day17.scm Normal file
View file

@ -0,0 +1,59 @@
(import (chicken io)
(chicken format)
srfi-1
srfi-42
srfi-152
matchable)
(define (read-input #!optional (port (current-input-port)))
(map (lambda (s) (map string->number (string-split (string-drop s 2) "..")))
(string-split (string-drop (read-line port) 13) ", ")))
(define-record state
x y vx vy)
(set! (record-printer state)
(lambda (st out)
(fprintf out "#,(state ~S ~S ~S ~S)" (state-x st) (state-y st) (state-vx st) (state-vy st))))
(define (too-far? target-area st)
(match-let ((((x1 x2) (y1 y2)) target-area)
(($ state x y vx vy) st))
(< y (min y1 y2))))
(define (in-target? target-area st)
(match-let ((((x1 x2) (y1 y2)) target-area)
(($ state x y vx vy) st))
(and (<= x1 x x2) (<= y1 y y2))))
(define (sign x)
(cond
((zero? x) 0)
((> x 0) 1)
((< x 0) -1)))
(define (step st)
(match-let ((($ state x y vx vy) st))
(make-state (+ x vx) (+ y vy)
(- vx (sign vx)) (- vy 1))))
(define (simulate target-area vx vy)
(let lp ((st (make-state 0 0 vx vy))
(prev-st (make-state 0 0 vx vy))
(max-height 0))
(cond
((in-target? target-area st) (list st (max max-height (state-y st))))
((too-far? target-area st) (list prev-st max-height))
(else (lp (step st) st (max max-height (state-y st)))))))
(define (find-max-height target-area)
(list-ec (: vx 0 500)
(: vy -100 1000)
(:let st (simulate target-area vx vy))
(if (in-target? target-area (car st)))
(cadr st)))
(let* ((target-area (read-input))
(max-heights (find-max-height target-area)))
(print (apply max max-heights))
(print (length max-heights)))

1
2021/day17/input Normal file
View file

@ -0,0 +1 @@
target area: x=236..262, y=-78..-58

1
2021/day17/test Normal file
View file

@ -0,0 +1 @@
target area: x=20..30, y=-10..-5

87
2021/day20/day20.scm Normal file
View file

@ -0,0 +1,87 @@
(import (chicken io)
(chicken format)
srfi-1
srfi-42
srfi-69
srfi-152
matchable)
(define (parse-line s)
(map (lambda (c) (eq? c #\#)) (string->list s)))
(define (read-input #!optional (port (current-input-port)))
(let* ((lines (read-lines port))
(algo (list->vector (parse-line (car lines))))
(img (map parse-line (cddr lines)))
(h (make-hash-table)))
(do-ec (: line (index i) img)
(: x (index j) line)
(if x)
(hash-table-set! h (list i j) #t))
(values algo h #f)))
(define (kernel img bg i j)
(fold-ec 0 (: k (- i 1) (+ i 2)) (: l (- j 1) (+ j 2))
(hash-table-ref/default img (list k l) bg)
(lambda (n acc) (+ (* 2 acc) (if n 1 0)))))
(define (enhance algo img bg)
(let* ((indices (hash-table-keys img))
(min-i (hash-table-fold img (lambda (k v acc) (min (car k) acc)) 0))
(max-i (hash-table-fold img (lambda (k v acc) (max (car k) acc)) 0))
(min-j (hash-table-fold img (lambda (k v acc) (min (cadr k) acc)) 0))
(max-j (hash-table-fold img (lambda (k v acc) (max (cadr k) acc)) 0))
(new-img (make-hash-table))
(new-bg (vector-ref algo (if bg 511 0))))
(do-ec (:range i (- min-i 1) (+ max-i 2))
(:range j (- min-j 1) (+ max-j 2))
(:let pixel (vector-ref algo (kernel img bg i j)))
(hash-table-set! new-img (list i j) pixel))
(values new-img new-bg)))
(define (count-pixels img)
(hash-table-fold img (lambda (k v acc) (if v (+ 1 acc) acc)) 0))
(define (part12 algo img bg times)
(if (zero? times)
(count-pixels img)
(let-values (((img bg) (enhance algo img bg)))
;; (with-output-to-file (sprintf "imgs/img~a.pbm" times)
;; (lambda () (img->pbm img bg)))
(part12 algo img bg (sub1 times)))))
(let-values (((algo img bg) (read-input)))
(print (part12 algo img bg 2))
(print (part12 algo img bg 50)))
(define (visualize img bg)
(let* ((indices (hash-table-keys img))
(min-i (hash-table-fold img (lambda (k v acc) (min (car k) acc)) 0))
(max-i (hash-table-fold img (lambda (k v acc) (max (car k) acc)) 0))
(min-j (hash-table-fold img (lambda (k v acc) (min (cadr k) acc)) 0))
(max-j (hash-table-fold img (lambda (k v acc) (max (cadr k) acc)) 0)))
(do-ec (:range i (index k) (- min-i 1) (+ max-i 2))
(:range j (index l) (- min-j 1) (+ max-j 2))
(begin
(when (and (not (= k 0)) (= l 0)) (display "\n"))
(if (hash-table-ref/default img (list i j) bg)
(display "#")
(display "."))))
(display "\n")))
(define (img->pbm img bg)
(let* ((indices (hash-table-keys img))
(min-i (hash-table-fold img (lambda (k v acc) (min (car k) acc)) 0))
(max-i (hash-table-fold img (lambda (k v acc) (max (car k) acc)) 0))
(min-j (hash-table-fold img (lambda (k v acc) (min (cadr k) acc)) 0))
(max-j (hash-table-fold img (lambda (k v acc) (max (cadr k) acc)) 0)))
(printf "P1 ~a ~a~n" (+ 2 (- max-i min-i)) (+ 2 (- max-j min-j)))
(do-ec (:range i (index k) (- min-i 1) (+ max-i 2))
(:range j (index l) (- min-j 1) (+ max-j 2))
(begin
(when (and (not (= k 0)) (= l 0)) (display "\n"))
(if (hash-table-ref/default img (list i j) bg)
(display "1")
(display "0"))))
(display "\n")))

102
2021/day20/input Normal file
View file

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

7
2021/day20/test Normal file
View file

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

46
2021/day21/day21.scm Normal file
View file

@ -0,0 +1,46 @@
(import (chicken io)
(chicken format)
srfi-1
srfi-42
srfi-69
srfi-152)
(define (read-input #!optional (port (current-input-port)))
(apply values (map (lambda (s) (string->number (string-drop s 28))) (read-lines port))))
(define (part1 start1 start2)
(let lp ((k 0)
(pos1 (- start1 1))
(pos2 (- start2 1))
(score1 0)
(score2 0))
(cond
((>= score1 1000) (* score2 k 3))
((>= score2 1000) (* score1 k 3))
((even? k) (let* ((dice (+ (* 9 k) 6))
(new-pos1 (remainder (+ pos1 dice) 10)))
(lp (+ k 1) new-pos1 pos2 (+ score1 new-pos1 1) score2)))
((odd? k) (let* ((dice (+ (* 9 k) 6))
(new-pos2 (remainder (+ pos2 dice) 10)))
(lp (+ k 1) pos1 new-pos2 score1 (+ score2 new-pos2 1)))))))
(define (part2 start1 start2)
(define cache (make-hash-table))
(define (play pos1 pos2 score1 score2)
(if (>= score2 21)
(list 0 1)
(fold-ec (list 0 0)
(: d1 '(1 2 3)) (: d2 '(1 2 3)) (: d3 '(1 2 3))
(:let new-pos1 (remainder (+ pos1 d1 d2 d3) 10))
(:let key (list pos2 new-pos1 score2 (+ score1 new-pos1 1)))
(reverse (if (hash-table-exists? cache key)
(hash-table-ref cache key)
(let ((val (apply play key)))
(hash-table-set! cache key val)
val)))
(lambda (x acc) (list (+ (car x) (car acc)) (+ (cadr x) (cadr acc)))))))
(apply max (play (- start1 1) (- start2 1) 0 0)))
(let-values (((start1 start2) (read-input)))
(print (part1 start1 start2))
(print (part2 start1 start2)))

2
2021/day21/input Normal file
View file

@ -0,0 +1,2 @@
Player 1 starting position: 8
Player 2 starting position: 3

2
2021/day21/test Normal file
View file

@ -0,0 +1,2 @@
Player 1 starting position: 4
Player 2 starting position: 8