2021 solutions

master
Matthew Butterick 3 years ago
parent 4d5f19b410
commit af8752d29e

@ -0,0 +1,20 @@
#lang br
(require racket/file rackunit)
(define depths (map string->number (file->lines "01.rktd")))
(define (positive-deltas depths)
(filter positive?
(for/list ([d1 (in-list depths)]
[d2 (in-list (cdr depths))])
(- d2 d1))))
(check-equal? (length (positive-deltas depths)) 1167)
(define (trios depths)
(for/list ([d1 (in-list depths)]
[d2 (in-list (cdr depths))]
[d3 (in-list (cddr depths))])
(+ d1 d2 d3)))
(check-equal? (length (positive-deltas (trios depths))) 1130)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,32 @@
#lang br
(require racket/file sugar/list rackunit)
(define instructions
(slice-at (for/list ([datum (in-port read (open-input-file "02.rktd"))])
datum) 2))
(define (solve matcher)
(for/fold ([pos 0]
[depth 0]
[aim 0]
#:result (* pos depth))
([i (in-list instructions)])
(matcher pos depth aim i)))
(define (solve-1)
(solve (λ (pos depth aim i)
(match i
[(list 'forward amt) (values (+ pos amt) depth aim)]
[(list 'down amt) (values pos (+ depth amt) aim)]
[(list 'up amt) (values pos (- depth amt) aim)]))))
(check-equal? (solve-1) 1488669)
(define (solve-2)
(solve (λ (pos depth aim i)
(match i
[(list 'forward amt) (values (+ pos amt) (+ depth (* aim amt)) aim)]
[(list 'down amt) (values pos depth (+ aim amt))]
[(list 'up amt) (values pos depth (- aim amt))]))))
(check-equal? (solve-2) 1176514794)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,40 @@
#lang br
(require racket/file rackunit)
(define lines (map string->list (file->lines "03.rktd")))
(define (digit-columns lines) (apply map list lines))
(define (most-common-digit chars)
(define zeroes (count (λ (c) (char=? c #\0)) chars))
(define most-threshold (/ (length chars) 2))
(cond
[(= zeroes most-threshold) #f]
[(> zeroes most-threshold) #\0]
[else #\1]))
(define (least-common-digit chars)
(match (most-common-digit chars)
[#false #false]
[#\0 #\1]
[_ #\0]))
(define (chars->binary-number chars)
(string->number (list->string chars) 2))
(define gamma-rate (chars->binary-number (map most-common-digit (digit-columns lines))))
(define epsilon-rate (chars->binary-number (map least-common-digit (digit-columns lines))))
(check-equal? (* gamma-rate epsilon-rate) 4174964)
(define (find-digit proc default)
(for/fold ([lines lines]
#:result (chars->binary-number (car lines)))
([i (in-range (length (car lines)))]
#:break (= (length lines) 1))
(define target (or (proc (list-ref (digit-columns lines) i)) default))
(filter (λ (line) (char=? (list-ref line i) target)) lines)))
(define oxygen-rate (find-digit most-common-digit #\1))
(define co2-rate (find-digit least-common-digit #\0))
(check-equal? (* oxygen-rate co2-rate) 4474944)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,43 @@
#lang br
(require racket/file sugar rackunit)
(define lines (file->lines "04.rktd"))
(define numbers-to-draw (map string->number (string-split (string-replace (car lines) "," " "))))
(define boards
(map list->vector (slice-at (map string->number (string-split (string-join (cdr lines)))) 25)))
(define winning-lines
(let ()
(define rows '((0 1 2 3 4) (5 6 7 8 9) (10 11 12 13 14) (15 16 17 18 19) (20 21 22 23 24)))
(define cols (apply map list rows))
(append rows cols)))
(define (board-wins board)
(for/or ([winning-line winning-lines])
(for/and ([idx winning-line])
(eq? (vector-ref board idx) #true))))
(define (run-game boards)
(for*/fold ([boards boards]
[winners null]
#:result (reverse winners))
([number numbers-to-draw]
#:break (empty? boards))
(for ([board boards])
(define pos-of-number (vector-member number board))
(when pos-of-number
(vector-set! board pos-of-number #true)))
(define-values (new-winners losers) (partition board-wins boards))
(values losers (append (for/list ([winner new-winners])
(cons number winner)) winners))))
(define (calc-score res)
(match res
[(cons last-number board) (* last-number (apply + (filter number? (vector->list board))))]))
(define results (run-game boards))
(check-equal? (calc-score (first results)) 64084)
(check-equal? (calc-score (last results)) 12833)

@ -0,0 +1,601 @@
27,14,70,7,85,66,65,57,68,23,33,78,4,84,25,18,43,71,76,61,34,82,93,74,26,15,83,64,2,35,19,97,32,47,6,51,99,20,77,75,56,73,80,86,55,36,13,95,52,63,79,72,9,10,16,8,69,11,50,54,81,22,45,1,12,88,44,17,62,0,96,94,31,90,39,92,37,40,5,98,24,38,46,21,30,49,41,87,91,60,48,29,59,89,3,42,58,53,67,28
31 23 52 26 8
27 89 37 80 46
97 19 63 34 79
13 59 45 12 73
42 25 22 6 39
27 71 24 3 0
79 42 32 72 62
99 52 11 92 33
38 22 16 44 39
35 26 76 49 58
39 19 82 53 57
52 98 69 77 23
1 40 18 66 83
34 85 28 48 16
15 93 38 96 27
74 50 88 84 99
34 2 11 25 17
57 4 19 83 1
59 77 42 36 33
73 22 23 37 55
98 91 56 84 78
45 21 24 83 40
46 58 8 67 4
33 97 55 7 86
2 68 64 27 69
68 29 14 49 26
4 21 87 71 32
58 5 17 46 93
45 96 8 83 2
78 91 9 20 42
49 81 19 48 37
38 23 45 82 92
93 99 67 66 42
40 74 25 56 16
21 47 26 75 61
53 66 72 30 34
55 82 77 6 92
60 56 8 22 88
5 71 49 29 74
28 2 32 84 73
52 31 24 68 41
48 82 19 29 65
51 91 97 39 80
3 55 43 40 38
20 89 53 45 75
29 74 19 89 18
32 88 93 46 63
91 4 94 64 5
57 54 49 36 40
97 81 39 77 1
7 57 94 84 39
92 3 28 15 75
88 45 65 81 63
86 4 89 37 71
8 13 66 42 85
60 66 35 47 98
96 27 40 51 39
3 64 25 28 74
58 17 97 59 29
95 31 18 44 37
3 31 97 85 71
79 82 22 61 98
87 14 17 66 75
36 89 88 83 63
44 8 81 25 48
73 84 28 90 94
25 19 44 10 23
8 59 17 9 93
20 77 97 64 6
98 82 27 70 91
18 51 16 99 2
58 22 89 13 19
39 66 91 8 32
49 24 85 94 42
45 70 10 86 4
23 81 66 13 34
25 80 97 5 42
79 35 2 78 9
0 6 91 94 45
21 90 76 50 56
50 92 2 96 75
85 82 80 97 31
61 35 55 27 56
74 42 9 29 90
86 15 88 47 1
18 20 54 92 62
45 22 32 61 75
1 38 50 81 42
82 4 21 77 65
27 51 56 39 48
36 10 62 28 70
94 99 34 54 6
15 1 41 13 12
92 52 2 63 82
90 64 29 69 32
23 77 33 90 17
45 78 5 67 28
57 73 89 81 21
49 64 37 15 14
7 59 4 43 16
81 92 25 28 90
93 72 43 94 26
24 9 13 74 10
21 2 36 32 51
87 97 55 86 71
82 71 99 17 90
69 95 65 55 10
9 92 39 62 78
59 13 61 24 44
8 31 58 0 57
17 83 55 99 27
79 4 33 76 7
81 43 44 49 72
2 48 97 20 77
47 60 35 16 63
93 95 94 1 98
61 57 84 55 22
85 40 65 46 59
21 15 63 77 7
13 99 49 3 96
8 21 14 45 41
65 63 82 62 28
91 44 22 79 96
20 75 86 3 26
74 11 42 59 36
5 52 43 92 99
46 63 10 45 81
13 66 21 32 89
25 28 96 40 88
27 18 31 73 34
3 26 43 32 36
68 87 67 65 99
73 61 20 90 7
21 52 2 82 10
58 49 56 16 80
97 25 93 63 32
87 14 5 22 76
89 92 91 3 51
0 24 95 69 20
96 11 10 1 55
95 86 44 75 70
59 76 45 2 99
1 34 71 81 41
87 14 33 84 96
8 38 9 82 68
27 71 70 75 76
25 87 2 79 96
20 88 50 37 32
48 94 63 86 22
15 6 34 78 59
30 89 51 31 77
74 10 86 71 84
29 54 58 44 5
11 90 26 50 63
64 62 20 40 46
37 9 46 23 31
68 21 25 36 90
17 33 6 50 30
11 89 20 47 60
26 59 34 62 77
84 52 40 97 7
88 30 42 58 94
64 10 2 90 83
44 35 77 91 47
14 74 9 78 53
86 14 0 39 24
87 69 58 8 73
88 74 27 40 51
63 54 55 93 61
16 66 15 21 48
43 70 9 81 42
36 54 99 34 95
98 19 90 25 44
69 56 18 77 49
58 16 67 75 57
36 44 14 98 23
31 5 83 46 3
45 21 41 11 60
33 81 88 92 65
13 51 48 59 71
12 5 70 87 32
42 18 90 73 88
68 29 76 38 55
67 62 15 77 34
39 27 51 54 19
87 8 92 93 88
77 54 15 1 43
35 97 26 21 29
13 46 96 69 47
51 38 91 32 63
73 99 30 15 16
42 58 21 88 44
45 13 27 68 9
36 6 81 53 5
78 76 11 60 1
57 76 50 78 31
45 42 68 53 16
9 88 89 19 21
96 61 97 69 34
98 87 33 82 0
4 16 89 57 64
46 75 77 65 23
71 42 96 52 38
1 21 93 0 35
59 80 53 36 58
97 62 35 1 88
98 60 17 45 94
12 43 65 23 19
71 52 3 40 59
50 76 61 20 22
92 65 38 93 13
55 26 10 46 29
85 23 19 74 34
60 14 27 36 18
53 4 52 49 17
99 56 93 70 28
25 0 77 80 57
91 50 72 76 23
53 58 95 78 59
75 85 90 44 9
30 8 5 60 6
28 35 59 70 96
20 99 98 81 79
94 78 27 71 4
7 34 43 46 51
93 65 22 69 33
92 49 75 35 11
58 39 62 86 83
64 4 76 48 82
74 1 56 95 31
1 78 98 90 55
80 14 36 99 7
85 8 10 9 92
76 11 40 70 62
43 53 74 35 58
46 78 35 28 49
84 73 65 25 34
40 59 66 36 67
16 22 29 0 45
20 56 39 88 91
32 58 35 25 79
78 94 57 38 14
89 87 68 48 76
7 67 40 51 33
95 31 43 93 92
38 21 82 31 23
54 16 77 37 42
73 99 7 34 90
71 26 5 91 52
22 27 47 85 62
2 86 28 37 55
1 82 9 36 31
52 98 89 30 60
13 17 63 38 57
73 50 42 20 12
56 3 67 62 35
59 39 19 22 27
21 58 57 41 54
75 13 82 50 32
23 5 99 66 10
7 19 45 66 78
38 57 40 73 87
58 30 99 53 83
64 1 8 56 95
70 77 16 18 82
72 83 95 37 35
54 59 92 21 79
7 81 86 29 41
52 99 42 57 71
3 15 75 34 77
7 70 5 69 4
34 60 40 73 6
74 54 67 32 38
93 62 17 51 86
57 88 99 3 16
42 74 11 34 7
82 47 71 31 58
69 23 43 4 64
32 19 98 93 41
63 97 8 85 48
63 54 34 38 86
4 27 15 49 0
61 77 53 98 74
62 23 88 97 37
93 28 25 50 13
56 82 41 27 79
23 31 64 7 65
52 98 93 16 57
88 49 10 11 62
43 95 53 51 83
41 10 87 54 86
19 22 13 40 17
37 27 45 29 63
83 85 81 90 7
57 88 47 66 56
67 44 54 88 89
20 46 61 28 92
86 49 60 83 95
42 78 97 51 96
11 62 4 26 31
18 68 87 26 70
62 84 11 33 90
0 45 66 83 6
20 19 27 44 55
52 8 5 7 3
54 94 88 76 92
13 98 22 33 26
95 62 53 81 24
29 69 15 87 25
61 40 84 90 93
7 31 3 28 46
20 51 21 18 38
30 92 39 70 61
27 88 35 96 74
23 5 66 11 42
40 61 90 57 54
41 14 99 62 59
92 10 48 81 52
22 29 77 18 87
31 79 25 94 13
17 26 44 98 57
74 83 51 14 11
76 91 96 64 33
43 45 92 72 27
66 3 28 20 40
88 82 44 71 55
83 47 51 76 24
86 19 42 34 99
30 31 87 48 62
98 53 68 9 21
3 31 6 41 61
24 77 81 96 44
78 73 1 98 11
40 80 27 65 92
62 67 2 30 10
78 46 50 65 56
84 16 32 58 86
22 12 54 99 35
9 43 55 10 94
66 81 59 92 76
78 3 55 23 83
13 42 94 91 22
14 37 31 67 71
8 61 57 34 43
74 50 0 39 65
78 16 13 91 34
14 74 86 3 97
12 89 58 65 51
29 57 48 44 93
95 1 42 39 92
93 96 16 85 25
59 3 70 19 17
21 84 58 38 86
57 10 35 95 79
81 44 73 63 9
22 1 96 7 93
40 49 2 4 66
87 21 17 32 48
44 28 42 99 26
69 8 85 86 75
21 31 37 87 28
89 43 74 83 57
95 29 92 88 35
94 25 97 81 50
15 19 73 45 63
92 62 67 95 57
30 8 4 39 64
99 31 70 63 96
25 53 24 93 35
34 51 82 91 28
41 30 20 56 46
16 32 98 60 35
67 9 43 42 88
78 90 71 5 29
49 31 37 63 18
80 40 88 5 62
3 6 74 71 97
19 58 63 59 38
50 64 34 68 45
25 30 21 33 83
10 65 67 17 50
21 51 18 68 59
29 78 77 99 76
62 35 96 7 95
82 53 42 49 69
74 65 89 6 1
18 30 72 75 24
60 50 52 55 82
68 99 4 61 22
9 37 84 57 87
96 85 56 72 2
9 38 98 12 4
34 45 74 97 86
18 94 64 70 68
91 41 58 39 66
34 13 26 80 29
0 4 21 60 90
39 73 12 2 19
64 44 61 88 45
59 50 8 91 49
34 85 55 2 75
10 15 89 12 63
90 29 87 73 71
38 17 84 45 9
97 98 77 23 61
47 43 22 58 1
63 44 2 94 99
33 81 51 49 13
38 86 42 91 23
7 67 68 39 84
4 26 12 38 41
43 16 88 71 99
50 24 19 77 98
23 73 44 10 51
56 42 30 52 59
57 16 9 62 27
26 65 56 10 82
0 74 78 12 99
77 18 38 5 37
7 60 40 90 23
14 69 18 51 8
21 79 60 36 12
68 44 59 45 16
90 50 85 25 70
91 31 30 54 26
24 40 51 72 63
31 60 62 25 96
9 44 35 28 91
97 4 34 81 2
61 68 94 52 86
0 57 95 88 94
36 38 25 35 19
13 6 8 61 98
45 85 86 69 97
41 32 7 15 59
41 82 19 29 34
44 96 6 91 76
69 21 32 94 98
4 10 88 30 2
8 74 56 65 99
36 91 73 15 54
62 55 40 27 44
11 60 95 61 46
31 32 21 41 35
74 86 83 89 79
2 96 94 82 68
39 83 49 30 15
62 11 86 99 59
51 80 12 72 58
87 66 98 53 29
44 71 18 63 85
11 75 60 66 13
36 9 94 57 8
10 12 32 3 86
4 29 54 70 21
27 33 76 83 67
77 29 65 39 44
52 34 25 93 64
35 4 57 92 84
41 51 88 96 0
21 91 82 3 26
23 8 36 20 73
54 39 60 34 57
49 99 97 69 43
41 93 95 80 63
73 77 4 9 22
17 33 15 86 79
38 16 99 98 30
64 92 76 50 68
83 85 52 87 88
57 53 13 36 76
7 10 91 3 22
8 84 56 73 59
62 80 85 38 33
68 97 47 14 96
36 8 98 43 70
85 95 31 1 51
33 41 78 89 56
76 16 15 34 82
12 18 39 4 3
98 49 41 30 95
68 89 81 48 84
15 19 90 66 14
32 1 88 34 64
73 65 6 20 86
22 18 13 74 34
75 4 60 88 46
25 97 54 94 91
42 67 40 11 81
5 12 49 48 15
82 91 18 73 57
97 50 34 16 66
29 43 81 20 15
19 44 85 4 32
90 58 39 53 42
50 53 83 10 0
93 16 84 23 13
89 63 75 69 51
65 35 67 56 70
4 37 29 47 38

@ -0,0 +1,38 @@
#lang br
(require racket/file sugar rackunit racket/set)
(define lines (map (λ (s) (map (λ (s2) (define ints (map string->number (string-split s2 ",")))
(+ (first ints) (* +i (second ints)))) (string-split s " -> "))) (file->lines "05.rktd")))
(define (Line-not-diagonal line)
(match line
[(list left right) (or (= (real-part left) (real-part right)) (= (imag-part left) (imag-part right)))]))
;; why does make-polar cause the solution to run faster?
;; both functions create an imaginary number
;; make-polar is slower to create the numbers (because it has to call trig functions)
;; but storing the polar numbers with frequency-hash is much faster
;; using inexact coefficients makes make-rectangular go faster
;; but still not as fast as make-polar
(define go-fast? #true)
(define imag-func (if go-fast? make-polar make-rectangular))
(define (expand line)
(match-define (list x1 x2) (map real-part line))
(match-define (list y1 y2) (map imag-part line))
(cond
[(= x1 x2)
(for/list ([i (in-range (apply min (list y1 y2)) (add1 (apply max (list y1 y2))))])
(imag-func x1 i))]
[(= y1 y2)
(for/list ([i (in-range (apply min (list x1 x2)) (add1 (apply max (list x1 x2))))])
(imag-func i y1))]
[else (for/list ([x (in-range x1 ((if (> x1 x2) sub1 add1) x2) (if (> x1 x2) -1 1))]
[y (in-range y1 ((if (> y1 y2) sub1 add1) y2) (if (> y1 y2) -1 1))])
(imag-func x y))]))
(define (calc-result points)
(length (filter (λ (x) (>= x 2)) (hash-values (time (frequency-hash points))))))
(check-equal? (calc-result (append-map expand (filter Line-not-diagonal lines))) 6113)
(check-equal? (calc-result (append-map expand lines)) 20373)

@ -0,0 +1,500 @@
309,347 -> 309,464
425,687 -> 300,687
226,766 -> 885,107
681,618 -> 921,378
968,54 -> 38,984
35,341 -> 321,627
493,485 -> 632,485
908,183 -> 110,981
677,378 -> 677,231
703,378 -> 703,536
179,581 -> 429,331
339,133 -> 664,458
212,680 -> 212,136
251,699 -> 858,699
163,725 -> 163,22
70,226 -> 97,226
968,119 -> 954,119
551,551 -> 415,551
768,167 -> 546,167
125,302 -> 155,332
640,201 -> 341,201
757,791 -> 757,736
406,570 -> 418,558
250,919 -> 976,193
570,362 -> 304,96
463,973 -> 463,337
322,199 -> 322,73
141,186 -> 141,906
964,940 -> 964,743
99,461 -> 15,461
255,856 -> 255,194
650,293 -> 650,136
89,98 -> 969,978
974,977 -> 37,40
641,795 -> 985,795
441,972 -> 441,427
18,942 -> 943,17
166,167 -> 617,167
182,146 -> 790,146
88,854 -> 928,14
537,38 -> 233,38
786,562 -> 867,562
251,102 -> 147,102
551,373 -> 672,252
915,713 -> 791,589
28,373 -> 28,651
463,365 -> 396,365
349,948 -> 737,948
246,860 -> 84,860
334,817 -> 866,285
880,958 -> 641,719
229,203 -> 740,714
39,220 -> 575,756
899,383 -> 275,383
49,952 -> 774,952
384,42 -> 581,42
11,731 -> 522,731
194,638 -> 679,153
922,279 -> 922,398
589,579 -> 709,579
97,716 -> 244,716
769,923 -> 769,189
636,567 -> 866,337
413,925 -> 729,925
581,524 -> 89,32
970,217 -> 851,217
716,373 -> 122,967
606,785 -> 191,785
322,432 -> 706,432
788,911 -> 788,889
904,917 -> 904,862
889,351 -> 796,351
508,18 -> 508,165
859,187 -> 879,187
531,409 -> 562,378
914,97 -> 233,778
194,191 -> 194,592
620,674 -> 55,109
194,973 -> 863,973
679,940 -> 679,296
836,108 -> 700,108
861,376 -> 585,376
166,299 -> 166,141
847,377 -> 847,217
872,972 -> 872,413
28,872 -> 28,695
876,152 -> 108,920
487,536 -> 697,536
30,28 -> 982,980
834,503 -> 834,586
927,459 -> 339,459
87,809 -> 770,126
24,973 -> 981,16
185,383 -> 185,13
850,328 -> 541,19
399,111 -> 742,111
703,305 -> 458,305
571,889 -> 803,657
356,697 -> 364,697
847,160 -> 108,899
170,954 -> 137,954
927,120 -> 897,150
687,662 -> 507,662
762,259 -> 762,951
90,612 -> 647,55
114,437 -> 483,68
138,269 -> 638,269
720,947 -> 29,947
563,52 -> 360,52
665,953 -> 187,475
283,855 -> 283,744
120,284 -> 120,319
935,422 -> 349,422
372,858 -> 372,779
68,768 -> 814,22
206,400 -> 22,400
72,954 -> 977,49
861,557 -> 794,557
893,654 -> 893,132
306,364 -> 697,364
828,165 -> 695,165
122,57 -> 986,921
509,470 -> 608,470
794,730 -> 520,456
291,305 -> 525,71
648,530 -> 92,530
329,173 -> 329,343
960,941 -> 133,114
256,523 -> 369,523
433,379 -> 195,379
199,783 -> 821,783
974,205 -> 299,205
200,400 -> 27,573
294,175 -> 294,493
320,20 -> 320,393
274,85 -> 969,780
112,73 -> 112,969
371,381 -> 121,631
942,906 -> 46,906
663,742 -> 208,287
422,258 -> 422,356
884,283 -> 859,283
750,142 -> 710,142
823,454 -> 642,273
296,366 -> 296,245
518,615 -> 852,949
74,513 -> 655,513
850,77 -> 850,950
985,980 -> 33,28
16,982 -> 979,19
265,234 -> 849,234
303,408 -> 229,334
344,63 -> 932,651
417,597 -> 548,597
729,361 -> 245,845
888,156 -> 80,964
215,29 -> 411,225
762,108 -> 115,108
63,855 -> 875,43
398,382 -> 874,858
419,78 -> 419,417
263,553 -> 131,553
766,399 -> 584,399
778,126 -> 226,678
580,781 -> 580,401
623,506 -> 966,506
364,723 -> 364,349
834,667 -> 177,10
402,515 -> 402,493
924,50 -> 22,952
64,826 -> 64,470
199,694 -> 145,694
893,900 -> 20,27
850,834 -> 725,959
47,573 -> 575,45
71,287 -> 71,296
796,728 -> 796,442
88,700 -> 726,700
230,332 -> 412,514
618,284 -> 618,661
221,738 -> 817,142
149,38 -> 474,38
563,331 -> 441,331
219,187 -> 522,187
300,341 -> 300,633
228,305 -> 70,463
396,875 -> 22,875
533,116 -> 519,116
257,781 -> 257,443
181,236 -> 181,822
10,13 -> 986,989
59,290 -> 753,984
121,89 -> 121,827
958,233 -> 653,233
685,641 -> 685,322
167,124 -> 446,403
246,170 -> 77,339
503,189 -> 503,72
666,182 -> 824,340
825,675 -> 629,479
967,915 -> 967,785
749,403 -> 92,403
950,217 -> 950,391
356,872 -> 514,872
279,900 -> 138,900
114,284 -> 502,672
700,792 -> 32,124
252,783 -> 806,229
557,215 -> 557,103
35,29 -> 963,957
650,285 -> 23,912
669,191 -> 446,414
66,283 -> 66,37
564,250 -> 175,250
425,611 -> 425,964
662,224 -> 707,224
599,979 -> 599,873
402,886 -> 402,979
329,181 -> 329,964
120,891 -> 685,326
788,438 -> 788,460
140,939 -> 338,939
496,343 -> 327,343
749,151 -> 749,339
181,527 -> 181,455
61,949 -> 966,44
138,262 -> 894,262
192,146 -> 801,146
301,405 -> 765,405
938,235 -> 938,55
543,958 -> 320,958
54,982 -> 867,169
66,147 -> 702,783
839,419 -> 97,419
519,879 -> 519,707
159,255 -> 159,787
258,897 -> 968,897
427,10 -> 427,62
782,750 -> 782,960
878,742 -> 785,649
171,74 -> 883,74
220,184 -> 910,874
696,984 -> 696,512
175,753 -> 303,753
666,515 -> 45,515
886,101 -> 14,973
121,823 -> 154,823
63,976 -> 987,52
480,478 -> 167,791
757,338 -> 757,719
593,286 -> 542,286
989,602 -> 989,135
793,857 -> 712,857
65,976 -> 843,198
729,334 -> 106,957
102,234 -> 42,294
830,223 -> 267,223
800,590 -> 921,590
526,38 -> 863,38
770,719 -> 65,14
317,267 -> 541,267
653,697 -> 653,720
506,532 -> 483,555
564,387 -> 205,387
971,669 -> 971,966
421,905 -> 421,264
506,85 -> 407,85
435,863 -> 230,863
945,133 -> 694,133
604,921 -> 604,168
66,677 -> 499,244
300,551 -> 893,551
836,228 -> 836,631
29,208 -> 443,208
546,584 -> 148,584
855,904 -> 855,315
636,694 -> 852,478
399,252 -> 399,170
46,596 -> 46,789
919,211 -> 201,929
662,983 -> 545,866
22,913 -> 908,27
441,605 -> 94,952
190,257 -> 769,836
700,395 -> 861,556
562,620 -> 562,687
34,165 -> 603,734
372,302 -> 585,302
71,857 -> 588,340
956,566 -> 738,784
778,610 -> 74,610
331,640 -> 346,655
274,473 -> 274,691
646,142 -> 144,142
911,971 -> 618,971
233,341 -> 233,505
467,990 -> 41,990
633,739 -> 57,163
585,405 -> 905,405
320,449 -> 320,628
44,738 -> 44,293
67,267 -> 770,970
933,155 -> 765,323
383,879 -> 896,366
130,986 -> 435,986
264,863 -> 979,148
114,721 -> 725,110
546,949 -> 546,790
762,42 -> 67,42
443,985 -> 245,985
689,803 -> 126,803
496,702 -> 943,255
955,963 -> 117,125
686,411 -> 979,704
226,256 -> 226,352
889,683 -> 889,437
47,161 -> 545,161
450,283 -> 450,469
461,338 -> 461,695
808,777 -> 808,962
902,459 -> 902,744
793,703 -> 158,68
100,919 -> 69,919
912,785 -> 331,204
712,609 -> 712,512
268,762 -> 268,355
972,667 -> 974,667
647,647 -> 164,647
589,180 -> 589,644
836,258 -> 376,718
676,977 -> 211,977
626,608 -> 874,360
271,911 -> 324,858
182,374 -> 182,347
14,989 -> 985,18
461,462 -> 956,957
82,79 -> 974,971
607,478 -> 607,147
898,76 -> 582,392
326,31 -> 683,31
768,47 -> 768,348
35,386 -> 185,386
803,391 -> 803,932
879,486 -> 879,658
183,39 -> 183,855
431,467 -> 499,399
434,306 -> 304,436
774,618 -> 521,618
364,426 -> 364,457
44,849 -> 791,102
70,850 -> 276,850
181,838 -> 181,736
574,18 -> 574,784
103,613 -> 537,179
34,218 -> 115,299
808,777 -> 636,777
483,112 -> 483,939
15,790 -> 15,253
433,427 -> 742,427
829,947 -> 895,947
361,180 -> 860,180
124,499 -> 124,615
879,712 -> 745,712
16,12 -> 16,149
36,981 -> 36,561
929,52 -> 30,951
845,85 -> 318,612
114,731 -> 794,51
434,280 -> 406,308
530,513 -> 114,513
417,715 -> 417,273
44,845 -> 44,225
951,122 -> 450,623
32,707 -> 32,832
51,58 -> 51,806
165,305 -> 49,189
517,221 -> 942,221
125,233 -> 193,233
903,180 -> 101,982
123,303 -> 247,179
199,174 -> 546,521
185,860 -> 538,860
825,751 -> 825,784
454,720 -> 64,720
28,10 -> 974,956
626,760 -> 586,760
91,234 -> 10,234
973,939 -> 65,31
589,308 -> 255,308
547,945 -> 239,945
909,914 -> 111,116
484,182 -> 253,182
145,575 -> 339,575
215,143 -> 611,143
963,983 -> 20,40
220,733 -> 333,846
126,860 -> 940,46
715,823 -> 715,284
832,65 -> 436,65
923,496 -> 530,889
708,517 -> 708,764
154,681 -> 22,549
909,135 -> 57,987
225,966 -> 225,941
629,491 -> 629,17
927,349 -> 72,349
15,987 -> 983,19
265,912 -> 74,912
14,985 -> 988,11
986,64 -> 129,921
697,831 -> 943,831
379,143 -> 853,617
232,887 -> 623,887
947,473 -> 947,453
898,762 -> 218,762
599,386 -> 870,386
757,137 -> 757,496
437,285 -> 437,326
515,311 -> 515,63
305,703 -> 720,703
321,770 -> 88,537
75,48 -> 457,430
38,499 -> 38,544
481,896 -> 481,944
614,483 -> 437,483
647,430 -> 368,430
641,669 -> 641,691
849,626 -> 427,204
805,688 -> 805,536
102,315 -> 102,108
729,525 -> 770,525
234,702 -> 38,702
17,457 -> 526,457
369,155 -> 369,647
216,118 -> 216,43
342,384 -> 342,905
470,832 -> 314,676
179,318 -> 179,315
40,707 -> 547,707
771,236 -> 453,236
113,823 -> 826,110
731,642 -> 707,642
36,398 -> 810,398
233,447 -> 979,447
74,286 -> 907,286
939,223 -> 939,10
866,57 -> 866,656
978,20 -> 10,988
816,176 -> 50,942
293,868 -> 293,350
900,159 -> 148,911
58,84 -> 644,84
720,416 -> 720,906
935,31 -> 13,953
41,727 -> 221,727
633,112 -> 633,695
418,947 -> 418,574
632,711 -> 791,711
73,228 -> 73,861
59,447 -> 83,447
418,938 -> 418,638
922,352 -> 636,352
66,773 -> 66,868
69,678 -> 600,147
333,251 -> 298,251
371,803 -> 740,434
976,972 -> 976,165
896,415 -> 240,415
672,476 -> 860,476
202,291 -> 195,291
99,971 -> 518,552
284,858 -> 910,232
187,282 -> 187,627
157,445 -> 157,665
421,879 -> 38,496
155,431 -> 405,431
772,472 -> 315,929
69,818 -> 132,818
70,328 -> 70,800
471,788 -> 646,788
960,900 -> 97,37
258,566 -> 186,494
345,413 -> 306,413
897,173 -> 897,896
74,740 -> 74,795
679,238 -> 679,811
870,64 -> 64,870
30,869 -> 288,869
539,380 -> 539,862
452,692 -> 748,692
527,712 -> 527,139
725,504 -> 717,504
201,338 -> 636,338
626,719 -> 626,302
580,153 -> 274,459
654,215 -> 246,215
363,738 -> 363,192
335,502 -> 970,502
266,52 -> 266,442
125,127 -> 281,127

@ -0,0 +1,18 @@
#lang br
(require racket/file sugar rackunit)
(define fish (map string->number (string-split (file->string "06.rktd") ",")))
(define (simulate fish days)
(for/fold ([freqs (frequency-hash fish)]
#:result (apply + (hash-values freqs)))
([d (in-range days)])
(hash-set*
(for/hasheq ([(k v) (in-hash freqs)]
#:unless (zero? k))
(values (sub1 k) v))
6 (+ (hash-ref freqs 7 0) (hash-ref freqs 0 0))
8 (hash-ref freqs 0 0))))
(check-equal? (simulate fish 80) 361169)
(check-equal? (simulate fish 256) 1634946868992)

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

@ -0,0 +1,13 @@
#lang br
(require racket/file sugar rackunit)
(define posns (map string->number (string-split (file->string "07.rktd") ",")))
(define/caching (gauss-summation x) (* (/ x 2) (+ x 1)))
(define (fuel-cost alignment [post-proc values])
(foldl (λ (posn res) (+ res (post-proc (abs (- alignment posn))))) 0 posns))
(define possible-alignments (remove-duplicates posns))
(check-equal? (apply min (map fuel-cost possible-alignments)) 349357)
(check-equal? (apply min (map (λ (pa) (fuel-cost pa gauss-summation)) possible-alignments)) 96708205)

@ -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,33,133,43,1060,890,12,15,136,42,25,96,694,522,893,204,204,1168,311,1046,1699,26,399,299,66,644,402,65,480,711,72,894,244,249,337,331,774,126,23,484,1299,662,404,235,86,1492,556,73,478,210,82,433,597,154,130,178,491,578,856,532,1191,544,256,831,252,1001,109,37,1290,317,376,22,742,496,930,118,28,376,73,247,942,895,38,675,138,387,203,271,104,65,1099,981,167,67,57,607,1095,202,225,1067,1757,324,127,785,266,518,135,914,1006,1402,578,28,548,211,673,302,525,208,115,92,514,518,71,1298,796,780,166,1341,475,273,101,1155,838,1219,901,727,497,168,543,416,174,31,755,865,106,358,236,186,369,550,465,617,375,535,1639,513,419,1377,1024,704,77,38,0,149,5,28,1163,149,1654,614,1201,89,1141,844,1390,1081,132,1385,52,1027,80,572,377,340,39,630,875,692,289,339,358,68,205,54,149,41,1208,1528,171,204,438,571,308,556,1372,426,204,18,31,51,40,287,1845,1721,441,240,875,901,328,800,341,59,530,134,275,11,7,7,1,1571,218,374,536,992,464,234,398,300,74,99,1163,1039,430,43,659,667,1115,407,257,717,657,249,46,109,734,67,1010,581,1070,738,478,621,183,224,1372,560,1573,1026,338,485,1138,1007,910,16,846,556,423,200,962,103,570,540,900,839,319,171,14,22,205,225,569,81,381,132,127,139,123,788,1571,35,830,65,677,1745,819,804,854,346,190,480,1500,76,1049,306,17,668,113,163,755,1015,718,1037,156,267,158,74,377,26,294,203,334,1186,88,384,853,404,290,135,620,668,234,1158,2,1102,137,884,287,15,638,1003,187,24,534,24,647,683,934,275,1844,887,1746,1614,1788,632,100,332,1565,1352,341,1027,475,958,289,1564,89,1138,233,535,790,990,863,889,45,44,169,251,522,11,41,104,45,828,1206,1515,645,39,544,382,1413,995,188,310,51,39,474,14,7,1387,809,428,77,8,867,1105,718,426,146,486,191,1251,677,1139,802,585,1140,46,39,128,867,49,33,198,731,349,661,296,103,22,444,1198,1149,188,245,492,1147,230,213,300,551,295,1313,365,975,587,1416,1213,233,257,631,564,876,434,1353,51,748,1179,1428,915,115,57,90,1312,892,200,1349,35,1010,445,619,1261,108,14,1424,481,381,209,154,23,972,646,593,6,289,171,543,97,28,401,290,298,14,37,1326,1177,533,67,75,294,328,527,449,455,176,345,226,729,210,55,45,0,834,887,123,1326,931,278,449,1278,608,217,411,143,447,16,1043,29,165,88,860,582,21,811,920,162,1788,15,423,1172,842,801,845,20,155,155,642,40,1036,560,348,689,328,505,84,1013,58,93,1653,233,233,383,380,84,617,1128,305,123,508,205,6,322,118,359,1186,84,677,640,80,1357,868,1035,8,64,995,1246,266,443,346,112,523,625,206,66,565,1878,25,1277,936,283,148,987,282,368,883,542,631,946,118,53,4,235,16,950,4,998,106,25,151,1013,27,1038,77,140,82,1119,236,125,947,1446,680,301,301,936,21,609,516,280,264,281,108,43,215,36,126,401,402,693,360,321,92,1809,305,551,86,77,278,81,524,400,1458,1342,897,49,35,518,288,655,91,398,38,251,647,79,400,151,520,459,960,425,663,298,584,90,533,690,610,755,56,19,21,244,548,1116,773,43,115,171,1127,103,1199,1470,176,451,693,65,186,262,963,137,1422,431,533,210,799,17,388,600,1113,2,181,815,1153,6,618,590,719,196,39,301,424,193,560,175,351,279,603,171,423,146,158,48,398,513,115,1,1051,817,200,473,143,261,435,856,1057,503,51,846,1020,177,1091,232,500,372,475,70,485,1227,1032,64,743,299,159,1077,18,204,944,1075,29,78,63,67,9,1007,354,1046,491,448,206,222,121,955,290,381,147,146,104,576,722,163,715,1475,130,1104,586,97,352,173,713,315,1482,1221,38,10,81,457,745,323,47,197,1012,1593,128,463,373,272,90,121,1248,1451,540,681,63,950,19,208,230,1362,1225,1500,207,81,739,288,626,261,1188,356,889,408,3,368,94,858,1512,834,43,5,833,826,33,791,800,39,299,1587,41,783,498,899,296,1189,470,66,307,892,47,207,199,902,17,14,1831,11,576,729,1436,153,142,81,165,214,1543,1464,561,737,180,162,515,867,65,74,200,9,11,539,19,305,996,334,297,1825,427,169,225,53,688,420,623,111,313,324,5,376,433,135,308,94

@ -0,0 +1,71 @@
#lang br
(require racket/file rackunit racket/dict)
(define (parse-ints strs) (map (λ (str) (string-split str)) strs))
(define recs
(let* ([lines (string-split (file->string "08.rktd") "\n")]
[sublines (map (λ (str) (string-split str "|")) lines)])
(map parse-ints sublines)))
(define strlens (append-map (λ (vals) (map string-length vals)) (map second recs)))
(check-equal? (count (λ (len) (memq len '(2 3 4 7))) strlens) 278)
(require csp racket/set)
(define (infer-config #:one one-digit #:four four-digit #:seven seven-digit #:five five-segment-digits)
(define prob (make-csp))
(add-vars! prob '(s1 s2 s3 s4 s5 s6 s7) '(a b c d e f g))
(add-all-diff-constraint! prob #:same eq?)
(add-constraint! prob (λ (s3 s6) (equal? (set s3 s6) one-digit)) '(s3 s6))
(add-constraint! prob
(λ (s1 s2 s3 s4 s5 s6 s7)
(define two-pattern (set s1 s3 s4 s5 s7))
(define three-pattern (set s1 s3 s4 s6 s7))
(define five-pattern (set s1 s2 s4 s6 s7))
(for/and ([pat (list two-pattern three-pattern five-pattern)])
(member pat five-segment-digits)))
'(s1 s2 s3 s4 s5 s6 s7))
(add-constraint! prob (λ (s2 s3 s4 s6) (equal? (set s2 s3 s4 s6) four-digit)) '(s2 s3 s4 s6))
(add-constraint! prob (λ (s1 s3 s6) (equal? (set s1 s3 s6) seven-digit)) '(s1 s3 s6))
(solve prob))
(define segments-table (list
(cons (set 's1 's2 's3 's5 's6 's7) 0)
(cons (set 's3 's6) 1)
(cons (set 's1 's3 's4 's5 's7) 2)
(cons (set 's1 's3 's4 's6 's7) 3)
(cons (set 's2 's3 's4 's6) 4)
(cons (set 's1 's2 's4 's6 's7) 5)
(cons (set 's1 's2 's4 's5 's6 's7) 6)
(cons (set 's1 's3 's6) 7)
(cons (set 's1 's2 's3 's4 's5 's6 's7) 8)
(cons (set 's1 's2 's3 's4 's6 's7) 9)))
(define (string->symbols str) (map (λ (c) (string->symbol (string c))) (string->list str)))
(define (wire-config strs)
(define encdoded-digits (map (compose1 list->set string->symbols) strs))
(define (filter-length len) (filter (λ (x) (= len (set-count x))) encdoded-digits))
(define one-digit (car (filter-length 2)))
(define four-digit (car (filter-length 4)))
(define seven-digit (car (filter-length 3)))
(define five-segment-digits (filter-length 5))
(infer-config #:one one-digit #:four four-digit #:seven seven-digit #:five five-segment-digits))
(define (decode-number config pattern)
(define config-reverse (for/hasheq ([(k v) (in-dict config)])
(values v k)))
(define segments (for/set ([item pattern])
(dict-ref config-reverse item)))
(dict-ref segments-table segments))
(define (output-value config strs)
(map (λ (pat) (decode-number config pat)) (map (compose1 list->set string->symbols) strs)))
(define (rec->output rec)
(for/sum ([digit (reverse (output-value (wire-config (first rec)) (second rec)))]
[power (in-naturals)])
(* digit (expt 10 power))))
(check-equal? (apply + (map rec->output recs)) 986179)

@ -0,0 +1,200 @@
fgcae ebafc cabdef eg abecfg abgfed feg gafdc bceg ebgcadf | defagbc faecg cfdag gecb
eagd cad fgadbc aefdcg dcebfg fcegd cbeaf ad dbgfeca defca | cfdeg gdcabf fcgde afgced
gfdeca aeb eb fbdag eafdc adfbe cefdab bdaegcf efbc ecadgb | eb dbafe eab faecd
facbdge efdg gcafd daegc caegb aecfdb ade ed gafbdc dgfeac | de abcdef faedcg dfgca
dcaegbf bc dcgeab cbgad cebg cdfaeb fbadg acdge cefdga cdb | gdcfae gbec gdace cgadfe
ceagfbd fbaeg fadge fbegad abgecf dafcg ed edab ged dfcbeg | gebfa de gbafec ed
acedbf adfbg cgafedb geac cgb befcgd bcgaf bfgace cg bfeac | bcdfeg fecba cafgeb bgdfa
egadc cea ac agbcedf cgab bgcade fbcaed eadfbg fegdc ebdag | fdecabg fcged cdbage efgdab
cbfge gdc cdea efdag dc cdfeg gfbcad gcdeaf cdeagbf egfadb | dfcge egcfd fcdbag dc
gaedb fbced gcdbef cge gc decgb dcbgfea cgfb cgdfea fbcade | fdegbac fecbd ecg degba
geaf bcfedg agbcd bfeagd dgfbe cbdaef beacdfg abe agebd ea | fbecdg geaf gebfad adgbc
bed afgebd adecf bafgedc dbca cfegb becdf fabdce bd gcfdea | gfdaeb ebfgad dbefc cbda
gacfebd ag bedfca agbfde egfcab dfga degab fbade bag bdecg | dgaf abcfed agb bfecag
afebg afdeg gcbe acebdf cfbage cafeb gab decgabf bg gcfdba | gb caebf abgef acfebd
efdab fabdgce gcbfa eg gacbfe beg ecag beafg dfabcg bgecfd | bge fgdcbe gcfdba ebdaf
aecbd bdagef gd bfega aefgcbd gfde gfdcab dbg edgba febagc | dg dagfbc dgfe eabdgf
gdfba egfacbd gdcefa baf gcba fdagcb dgfac ab dcfeba ebgdf | dcfag bfa gafcd cbag
afbe bdagc cedfb defcab ebfdgac gfedbc cbfad af adgcfe adf | dafbc dfbac fdcegb gadcb
fcgedb ca fdcage cagdbef ceab acedfb fdbce cafdb cda afgbd | ac dcafb efgdac ebcfda
fbad gbcda dcefgb cfeadg dgbcaf bcafdeg ad gcabe adc dfbcg | gbcdef bcfdg cgbfd bfadgc
afcgeb bedag bdecafg agb bedfga fdagce bg bgfd dfgea cdabe | badge cagdfe efdag afegbd
gfed dcaef ge bafdce daceg acebfg cefadg egc ecdabfg gacdb | bcgad bcegaf agecd degcafb
deacb adcge ab acb fbdce faedbgc fgadec gabd deabgc ecbfag | dfbec ab cbagfe bcfgae
gcabfde ecf cbdeg bacgf fedg ecfgb gfedcb fcaedb cebdag fe | dbcegf bcgdfe gfde defg
daefb bfg gedafb fabge dbafec cfega bdga cgbdfe gb gbacedf | aecfg bcedaf begdcf dgba
bagce bf bdacfeg efbg fcb cefab gbfadc cafebg ecdfa bacdge | fegb cegba bagce efbg
becag cfeg cefgbad fbace gfeacb acdegb bef ef adbcf fdabge | ef bdfac cbegad gbeac
egb afcbe fbaeg eabgfdc gcab gb gfdea fgbecd bcdafe gafbec | fcdgeb edcgbfa gbe fgeba
dga caebd ecdag cegfd abdfce dafbge ecbgda bcdgefa ga agbc | afdbec ecfdab gdaec gad
fc gbdefc cfe ebdaf fcbeda gbafde edacg bfcdgae dacef cabf | decgfba cdbefg cf ecdabf
febgd dafgbc caed cgd ceadgf fbecga dcgfe aegfc gbedfca cd | dgfeb fgced dcafebg agdfce
ecdafgb bgade bgd fabd gbaef bd egadbf ceadg cfbgea dgbfec | edagc bdage bfda dbgae
cgeba aefgcd dfceagb dc ced facbed cdfb dbaec adfeb fgedba | fbdea bgcae efbda fcdeag
caegbf dcfga afcedgb afgdcb fcedag ab edbcg dbfa gdcab abg | bag cbdag agb ab
gadfecb befc fgdbea cf fedagc fac bcgfa agbfe adgcb feagcb | gbcda cgbad bcgfa cf
dae aefcb dgabec dacfg afebdc ebfd afecgb aedcf fbcagde ed | ecabfgd bedf bcadef egdacfb
abcf fcega fdgce fegbda eafdcgb ac gabecd gbfae beagfc eca | edbgca edbfga cae gface
bgedcf afcdbg ag gcbaef fcedgab afgdb cfgbd befda acdg agf | fdbga ceadbfg egfbac fag
bad edafc bdeg abcgde ecafgbd gfadcb bgaec db edcba geafcb | cdafe fdcabg cdfageb dbge
geb bgcf agfcde gb befag gfbcae defab gabdec eacfg gabecfd | afbedgc gceabf abgef egacf
fdeac acg cfegda cbeadf fegdbca ag acefg adge bfdgca gbcfe | gecdaf efgca cgbafd cfgeb
begdf bdfgc ebcf adfcg cdegbf dgeafb cbg cb bfeagdc gbdcea | acgedb fagdbe bgfde dfgac
afcgdb aedc bedgf egafcd afdge gecaf fcgebda gda da ceabfg | gedbf gacfe adg agdef
bdeafg acgebdf dgcfa fa cafe dgfbc gaf edagfc cgabde agedc | fadbge bdgfcea dgcefa cgeda
gacfbde defag cbgef gbca efadcb afcbeg cgbdfe ba eba gefab | fegab abe ebfcgd fbgae
gfbdec fbcade cabdegf ebdg bfd dcfgb cadgf gebcf db fcabge | eacdgfb bdge ebdg ecafdb
egcfba fdcae fcdaeb gcfdae fabd ba cdeab fcdbaeg eba decbg | cabed facbed cbade dfcea
agdfe acfdeg fcdbeg dgf gdaec df cadf ceagbfd abgfe gbdaec | bgcdef abfge gbefcd df
dgebf afgdb bfa fcbade af dcafgb agcf gcbda dabgecf gadecb | dbcga abcdge fadgbce eagcbd
dbcga bea egacdf dageb be efbd abgfce gadfe fcbegad aebfdg | eba be bdage bedga
gcbd egbdafc fgc fbgedc gc aedbfg cefgb fedbg cfaegd efabc | bface egfbc bgdacfe gc
fdgae dc ced gbcd gbcefd decfba becfg ecfabg cgfde gbcadfe | edbcfg cd cafgbe cdegbf
geadcf efg gadef fg fecad acbgfe dagecbf dcgf gadbe afbdce | edafcg bgcfade gfdc abedg
edcba ecgdb aedbcf aebf dabfc fdacge badgfc ae ead gfcbade | gcebdaf degbc ebfadc efdcba
caedbg dbgec dcbf ecgdbf cgdef dgf bgaedcf egacf afebgd fd | cdfbeg ecfbgda gcbfed cbgedf
bdag acdeb ecgdb egbcfa fcdbega gb dbecag cbdafe ebg gdcef | bedgc abdfcge efagcb cdgef
bfade bfdga dfbgec cagbfd dfgacbe gba gacf ag gdcbf abgedc | cfag abdcge ebafd abefd
agcdef bcfda bdgca aebdfcg eagdc gb dbg gbea cedagb bfgdec | dfabgec eafdgc acdge gdecab
ebacdfg cgbfad egdbc bdgfa daef bae gabed febadg ae gacbef | fagdb daebgf fgdab bea
dcgbf cbedf degc bgc fcgdbea fbadg cfdgbe gc efacgb fdbace | edfcb cfeabg cg edbacf
bcdagfe dfacb cbfdge bgcdf fdgeac acd agbd acbef da dcgfba | cbgdf dgab ad gbdcaf
edgfbca gbe be ceba bdaecg gefcd dbagc egbadf dcegb dfgabc | gcfde gfceabd gfbcad eagfdb
dfcbg fcdga efgb gecbfd gecbda dfbgaec bedfc dgb bfadce bg | dbcgf bfge dacgf dfgcbe
cga ebdgaf bfdag cbgfe bfadgc gbfeadc ac cagfb cdba cgadef | bfdag agedcf aegcbfd gcfab
gcead gfdbec cbdge gabfce eb dfgcba ecfdgab befd dgcbf ebg | cdgae fbdegc bcdge gcdfb
da ebgfda fcdeagb acgbfe gedba dbegc agdecf dae bafeg bfda | gdafec fegba edfbga aed
fbagec gdfba bcg gebcfd cbafg bcdfgea ebac cefag faedcg bc | gfbac baegcfd fabegc dgeacbf
dfbeag fabdg fdbe abdeg eb dcega gcefba cfdbag dgcfbae geb | cgbafd bdef dcage eb
abdcfe gba dagf cagbefd fdbea bfega ag dafbge dcbeag gfceb | afbcde ecgfb dfbega bdgeca
dg dge gdbc cfdae bcgae cbdage abgfde cfedagb cdgae eacgbf | eabgc bgeac bcdg cagbe
bfd dgaeb gcabde fd bfgce dgfa dbefg gadbfce dbafge fabcde | edfcba efbcg df bdf
gcbfd agbecd fedc edcbfg fdb eadfbg bcfag fd gedcb gbacdef | fgbac bdagfec gfbdaec aegbdc
cgbdfe ce acedg adcfeg bcfegad fcea ceg abdeg afbdcg fcgda | ecg aefdcg dacge ec
egbdfca efacb agcdbe cbaed bdafgc dfaecg debg bd bda agced | geadbc efbca gabfdc bd
edcfb bfgecd ecabfd eca abcd fgeab fedcag abfedgc ac bfeca | bgfae eca cdgfeb bfaec
cadef cebdga ebd bacdfe dagfec dbfa fcebg ecbgfad db fdecb | befcd acgdeb abdf dbfa
gbfecda bfd cdagb fbdacg fgdbec gbaf cbadf aefdc eadcgb bf | adcgb dfbca agbf dafcb
abedg acbgf fd gaefcb cagbdef cdaf dcbfeg bdagf dgcbfa bfd | gbadcef df fadgb gabcdf
fbgadc dbcfg dcega cgdfe cfedbg caefgb ef gef gfaebcd fedb | fcedg febd gbcdfa cdgfab
bg adgb fcaedbg cgfda fcbae bcg dfacge fgcdba decgfb bcagf | cgafb dbfcage bdgcfe dbga
ed afbdg acefb aecbfg cfdabeg gedfcb fcdabe ecda def ebdfa | gbafd gedfcb dfegcb dfgba
gcdfae efgdb abdfcg fde gdabf dbcegfa fedgab efab ef ebdcg | eafb gbfda gacedf cbdge
edagfc gfaecbd bgced cbegfd cb gefdc badgfc bdc efbc adegb | dgbec cegafd dcbgef dgecb
gedcab beg eg cafgb fbegc gdfe bdgfec gabcedf bdafce bcdfe | gcefb afcbed ge gdfe
fdbagc cdgefab edacf ceba ea cfbaed ecfdg abfegd adfbc eda | ea beadfc abcgdef ecdgf
fbc gfbacd fecda cbdfa dbgf dceabg gbcdfea efbgac cdabg bf | gbdf agbced gaedbc gfdb
aegcb bfgecd fbegad fcagdbe dfgce fcbd gdeacf fb bgf bgfce | bgafedc gcdaef dgfcbe eadgcf
def dbce agdfeb ed fdgbcea dgcfb aegfc dfgbac gecfd cdfgbe | cdfegb ed cdgfaeb ebdc
ecbfad afdb eagbfc caebgdf edfcb dgfec cgdbae bd bcafe bdc | bfacegd fbdcae dbc bgfdcea
dfgba eg cfbead gde bfedgc dceab cabfdge gdbae aecg aedgcb | egd adebg gde debcfa
cadfb egfb afgbc adbegc ecgadf ecagb fga ecfgab bgacfde fg | afcbg cgfba gf dacbge
cegbf dec efcdg facdge dfae dcbafge ed adgfc fdbagc cgaebd | dce gbcfe afcdgbe dcgabf
cfabe fa eabgfc cgaf fba gfbce dcfbge edbgcfa efbadg acebd | cfebga bfa fedcagb abfce
acdfb dbagf afcebgd beag afgedb gfdea gbd fgdcbe gb cdeagf | dafge gafed gfacde gb
gdfeac dfgce ebfadcg edagc dacf baegdc fd gdf egcfb dabgef | degfbac cfdeg daebgf cdeagb
fcgabe ebcd ecfdg dabcgf gfecabd cfd ebcfg dc bcfegd adefg | agedf cd bcefg dc
cbaeg bcfea fb feb gfaceb gdfbec dagbec gfab deacf bgcaedf | acbfge bf afgb efb
egdba cadgfbe efadbg cg begc bfgdca caegd abcegd dcg acdfe | fbegcad bceg gcd cdgebaf
agbcd dafcgb cfdg ceadb fdgeba eafbcg gfdab gbc bgdcafe cg | gfadcbe fbagd decba gc
gfbec adgc dge egdcfa abgfde abgdfce dg eacfdb fdaec gdfce | fecgd gd fgcbe acdef
fdbceg bfedc af fegcad cbdag daf efab dabcef fbeadgc cbfad | fcabd cbfad gcfdbe gadbc
ebgac fabdeg bedca cg aegdcfb geacbf ceg efgba facg fbedcg | gecdfb aebfg fagc ceg
eagb dcgeb ba abd cbeagd cdgafb fecbdga fgedcb dacfe dacbe | bdfeacg cadgbf bgedfc fcdae
dbac cfgae cbfgde ba bga gcfbad dcfebga gcdbf feagbd cgbaf | cgafb fagbdc dcfbg afgebd
adbgfe fgb egdafc cbafd dfegbc edgafbc egfcd cegb gfcdb gb | bg fdacge efcadg egdfba
agfbe cgaefdb gbdf edagb afg cdgafe gfedab fg egcdba fabec | fcbdega fag agf agf
cae edabfc cdfega ca cfba gadeb fdecb ecagfbd cbgdef acdeb | facdeb fgecad bfadce efcgbad
facb dbfec dceabf efgcd cb aecfbgd dgfbae eafdb ebdagc bcd | bfedc cdegab cbdfe adefb
fecbga badgc dga bfgadec dg geacb cgedba dfebag gcde fcabd | decg gda baceg gd
gecbd gcbdfea cbfg bg bge egbcdf dcbef beafdc cdeag bfgeda | agcde dceag gfabde beg
gefba gacfbe facdeg cg cgab gfc efbdcag cfgeb cefbd afegbd | gfc afbge dafegb aebcgf
egd aebfgd gefc ge gedbfca bdcfe dfcegb gecdb edbacf cdbag | dacbg edcfbg bcdfe ge
df ebdga gcdefa cfbd bgdfa afgcb dfeagbc bfacgd fgd facebg | gaebd gecbaf fdcb dfbag
fag cegfdab cadebg dabcgf abefg eafc ecafgb ebacg fbdge fa | ebfag dbcgfae gbecad fdbgac
gecbd gefa gfc fabdcg bfaecd ecgafbd fg befgc efabc gecbfa | gfae gcf egbdafc edgbc
bfgae fcebag fdbe fd fedag adgfbc fdbgea ecagd agcbfed fad | fgabe dfeb ebfd gefba
ecdgfb afge bdage ebcda eg abdgef fgadb gabcfd dge bcdagef | gdebfa eg agfbdec afeg
dbc acefd cegadbf fbcde afcb fdcaeg bc acdbge febdac debfg | ceafd caefd gfbaced dcb
fecgb fgacd gdcefba de gbafce febd gecfd edc fecdbg ecbgda | becdfg ed becgad gcdeba
agfdbe fbac bcd edacfb bgecfd cb cdeba cfgdeab acged edfab | cb dbc adbce bdfae
dgacef bdfa ecgbf ab fegba dgafe efdbgca abg acebdg afbdge | gefda ecgafd ecfbg begcad
gbfcea bgfa afdebc aegdcb af bgcdfea bgeac gecdf afc fgace | bfgecad fca afc abfg
cfe dgcfe fdagcb gebdc egfa gbcdefa dcebfa fe agdfc fgaedc | dfgac fe fce fcdbega
fecgab gcbdef cbaefgd dfgbe dgcfe eb bfe dbfga edbc acfegd | fbdeg fdgeca agefbc egcfd
aecd bac dfacbe cafdb ac gefcab gfcdbe fcbde gbdaf cbdegaf | gcdbefa fbgcae ecad fagecb
eagbf cagedf geafdbc fdgbce adefb aecfd adcb deb faecbd bd | ebd deabf fadcge cdagfe
gadefcb bfcga eadcgb egdbfc deag ecg edabc acedbf eg abgce | gdea bagce gce gce
agcfe bec gecab gbeda facb dbfgec fcegab gfacde gfdaceb cb | cb cefdgb cadefgb bc
fdgb df egabcf fegda cedga fegab efdbac def cgdefba gafedb | facdbe bafceg agedf fdebga
fdaecg dbcafg abfgec dc caefg cedg aecfd abdgfce eadbf fcd | cgefab gbfacd fbegac fabecg
dfecabg bcfeag gdafce dagbfc cdefb dca gafbc dbag da bcadf | dca bagfce cbfda cdagbf
dabfgec febadg gbcdae gfbe adebf ef cabdf edfgca ebgda eaf | gfacbed abgced egdfca egfb
cfdea dc fdagebc fegcbd agdc gcfade fcabe cfd efdgba gfead | bdfcge agdc dgefa cfd
gbfca febadcg bc bdegcf beca begaf gabedf cgfbae cgfad cbg | bcg dcaefgb dfgceb afebdg
cg cgbf bedcg fcagde adefbcg efdgcb ebacd cgd edbfg egdafb | cfgedb bgcf acegdf cfgb
bfad adcgbe faceg fagbdc df afdgc efcbdag dfbegc gfd badcg | gfd abgcd fgd abfd
bcadgf cgdfb adgcb gf gafc dcfeabg bdagce dgf dfbega cebdf | adcbfg gfca fdcbe gfac
daebf fcabgde bgdcef gab ga edbcga afcg ebfga aefbgc egbcf | ebcfg ebcgfa fcgebd agcf
gedab fg afgecb cfgd feg cfabed befdc fgdcbe gdceafb befgd | ecafbg ecfbd febdc gf
gcbadf ce gfcdb fec feabd gbecdf dbcfage febdc agcdfe egbc | bafed gcbe gefcbd egcb
cbdfg gbcafd feb gaecf gcbfe gfbdae be fcgabde bdgcef becd | abgedf gdfebc aedgfb fbe
edacgbf fbdae edgac bg gab bagdec dagfce fadbgc cebg bgdea | aedgb bg cebg adcge
bda gbfea gedcab gfbceda bgdfa bfcdg febcdg bagcfd cfad da | dbecag bgfdc gdbfa facd
ceabd dabfe acgde bc fbca cgbfde febgda dfecagb ebafdc bdc | cbdae dbfea fgdceb gbfedc
acg fadbgce cfgdeb dbgce efdca ag dbga cagbef egacd cdgbea | gdecabf gcbfea ecbdg gac
dfgacb ged egfdab efcgbda gcefbd debga ebadc ge aegf gfdab | cdfgab cdafgb bfcgda ge
gdbeca bcfde eafg cefbgad dgafcb ge degfb adefbg dgfba bge | bdfeg begdf ebfadg eabdfgc
cfbdgae gbaec bagfcd gfcba dcaeg bacfeg dbeacf bec efgb eb | ceb egcab cagfb dgcae
fdcagb gdaecb fcadg bcdgaef gd dfcea gbcaf aecgfb cgd dgbf | gfadc gfdb badgfc gbfca
bcafed defgcab fgcb cfbae cagde agebfc gbcae gbe gadefb gb | bg gfcaeb bg aedgc
bcfadeg gbfd eacfbd gdafe afebg bafegd geafcd gebca fb bfa | agdefc eafbdg fgead gedaf
fcgeabd dgeaf ebgfad gceafb geadc ac aec ebcdg cafd adgefc | gbfadec egcbd acfd dgabef
eac fbdgec ea eadb cbaeg agfdecb eafdgc dcgbe bcfga adcbeg | aec abde eac ea
cedb gcadbe ed edgfbac fbega abdge gacdb fdacgb dcefag aed | cdagb gbcafd gdefac gedafc
cae agfbec ae begcd feab fadgbc ecfadg ecabg cdgbefa cabfg | ecgba afcgb efba dgeafc
fecgabd badf efadg abedfg fea fdgceb fedgb aebfgc af gdcae | ebfgca fae fea af
bgedac gbfae dcbefg acbf fb gaedf cagedbf aebgc gebfca fgb | aedgf fdgea bgfae bfdgeac
bagcfe eadcb gb facegdb bgc fdcag efbcad beadgc bgadc ebgd | gbc fbcage becad gbfcae
cagde gbca dcgbea gc efcad dbgfea dfgecb caedbfg adgbe cdg | gc egdfbc bcga cg
efgcbd aebgcd dfe eadfb abged abdcefg bacfd ef bdfaeg faeg | fgae cgdaeb adefb afcbd
cgbfea gcbda gefdcb eadf febag bdf fd febgad fadbegc bgadf | gebcfd fgdbea dfb dbf
dg cdgfe gecfad dfbce bgefda fgd eafcg efcbag dgac bcdagfe | afdebg gfd aecgf gfcea
edfga cgefda gcdbe fbae ab egdabf cfgbda deabg bad adefcgb | ebadg agdeb fbae gbdae
gbdcfa gbade ebgac ecbfa adfgbec badecf bgc gcef fbgace gc | gc fcge gacefdb ebgca
acefgd ebgaf egfda egcd bgfaecd dgcfa ed abcefd efd abdgcf | def ebgaf fed befcda
fecag cag cfagbd agfcbde fabec eabg fdcge cgbfea fadebc ag | cfgbda dfgbeca efdcg dfgcaeb
bgecfda ecdag dgfcbe bdgac adbgf afgdbe bfac gdcabf cbd bc | bc acbgefd fagcdb fcba
ba efacg cfbgde cfdbg bacgf bgfdea abf bcad cfbeadg bgcfad | bcdgef fdgaeb ab cagfe
cbdaf bafdgc eacbd fd dcf fcabg bcdfeg bedgcfa dgfa agbecf | dfga bgfac df agdf
edcabf cadbf bgfdcea bgcfad bgcde dfg fg bdfgc cgadfe abfg | adfecg gfbcd acgdfeb cbgdaf
badgfc dg bagcde bceda dbcfea beafg debag adg fcbadeg edgc | gebda dcagbf dceba egfab
edcf dacfbg dbcefag fbd df bdegfc ebdagc bgfea begcd bfedg | dgebc cbgde dgfbac bdf
bdcfg acegfb acebfd fdcag egadcf da daf cgeaf edag bafcegd | gdcaf fgaec bafdec fdaecb
cagefd fde gdfba fcgaedb abefd ef dafbeg ebfg afcbgd ebdca | egdbfa bceda dfabg bdace
fcgb gcdae bg gfadeb cbfegd fcabed bdg gcdbe fdcbe adbefcg | dgb gfbdec befdc gfbc
fabde bgfde bgecda cedgb gf cabfdeg ebfdgc fgbeac fcdg gfb | fgbde efdgb edgbc afedb
ebacf cedfab fde eabfd fd baecgf bagde agdebfc dfcb egfadc | ebfdgac acdegf abdefc ebfad
gdcb cd cfgae cgbedf fbegd efgdc fadcbe gfeacdb dce dfagbe | gbcd cde gacfe gefdbc
geb afedgb bfegca bgade eg dgef gadbc eafdb afebdcg fcbead | cagbd defcba dbafec geafcb
fgbdc gbfade gba gefa dbafg cbadge bfgecda ga edbafc dfabe | afbdeg dgfba bgdfa dcfgb
agef badce ef febad cabfdg gdfab cbgeafd egfbdc defgba fbe | abefd geaf gafdbe ebcad
gd gad bfdg gecba fedabcg fcgeda aedfbc adebf agbdfe dgeab | ebcdaf agd gcfbdae egacb
cedbfa ba cgbfa abf dfegac cafeg gabe befgca ebdfcag cbfdg | fab aegcf faecbd fdcebga
daefg febdga aegfdbc efgdca eafb bge eb agbdec febgd gbdfc | gbcfd edbfg egb bacegd
fbdecg bdafec cfaeb gedac def dceaf abfd fd gfecba gfdaecb | ebfac bdfa gbcdef fd
fcbga cbdfeag bafgce fdacg dc fcd fcgbad fadge efcdgb cdab | gaefcb bafegcd acdb abcgf
ecgabd acgdf abge ae bcfead fgdaceb ead daegc bcdefg gcbed | egba gaecd cgadbe gdbce
aegfbc gf afebdc agebdfc ebcdf feg dcefg bdfg dcgea bgedcf | edbfcg fdgb bdgafce febgcd
dcg bcdefa dbfceg fdeacg acbeg bfeagcd cfdae dg dfag agdec | dfcgea dg egdca edagc
agcfeb fagbed fdbceg bag gebfd afgd agbfcde acbed ag badeg | cbdea fcedgb gfebac abcegf
adbegfc fgdace cadge afcg ebdfca ebgad eac bfgdce cgefd ca | acged fcedg ebagd dgacefb
gfeadb gfeab bcfa agebcfd aebgfc fecgbd efc fc gadec egcfa | fgbace fc debgcf afgceb
bacg ab cfgbd bfadge acfed fgdecb agedcbf fcabgd bfa dbafc | gfdcb cbga abcg afecbdg
ge beagc cfgabd bgaced bgcda fbadgec age ecdagf egbd fcbae | aeg gedbac cadfeg fbgcda
dgbafe abfdgec gdf fd agebcf gadec dfcgba befd edfga aebfg | abcgefd dagec dfg df
edfcga bcegd baeg cbgdae agdbc gce acgdebf eg gfadbc cefbd | dcbeg bgedc cedbg edcbg
adfbe dg cagef dfgb gdbeca aefdg gda edcbaf fdgeba gdecbfa | cdabef eafbd bafcegd dg
cd edcbfg dbage fcaebdg facd bgcfea afbedc afbec dbc adecb | adegb bcefag abegd cbgedf
gdcbf da ecgbfda adc adgb bagcfd dfbcea egfac dfbceg gdacf | gdba cfdbg adbfec fcaeg
bcg bc gdcbae dbca abfgde gdeba ebfacdg egcba faceg dgfbce | geadb badecg ecbfgd baecg

@ -0,0 +1,36 @@
#lang br
(require racket/file rackunit)
(define depths
(for*/hasheqv ([(line lidx) (in-indexed (file->lines "09.rktd"))]
[(depth cidx) (in-indexed line)])
(values (make-rectangular cidx lidx) (string->number (string depth)))))
(define (neighbors loc)
(for*/list ([delta '(1 -1)]
[ifactor '(1 +i)])
(+ loc (* delta ifactor))))
(define (low? loc)
(< (hash-ref depths loc)
(apply min (filter-map (λ (li) (hash-ref depths li #f)) (neighbors loc)))))
(define low-locs (for/list ([(loc depth) (in-hash depths)]
#:when (low? loc))
loc))
(check-equal? (apply + (map (λ (lp) (add1 (hash-ref depths lp))) low-locs)) 539)
(define (higher-adjacent loc)
(for/list ([neighbor (neighbors loc)]
#:when (and
(hash-has-key? depths neighbor)
(not (= (hash-ref depths neighbor) 9))
(< (hash-ref depths loc) (hash-ref depths neighbor))))
neighbor))
(define (basin-at loc)
(remove-duplicates (cons loc (append-map basin-at (higher-adjacent loc)))))
(define basin-sizes (map length (map basin-at low-locs)))
(check-equal? (apply * (take (sort basin-sizes >) 3)) 736920)

@ -0,0 +1,100 @@
9976786789439313678999989767678999865435679398654323678999987654313468954569865334568916987643236789
9754395678998754599898978954567999976556789498775413478998798743203457893479973212479425698432145799
9843234589899866698786767943456789987687899987653104567897659957312368931298984596599934679549234689
8654785789789979897645459892967895698798999999873215698958349876433456892987899989989895998998965899
9865696895678989999731346799898954569999888921954524899542129986556767999976998679767699767897896789
9876789934799998997890235689799962978999767939878436789674398898668979998865797598755589656656897994
9997898956789987976531345679677899899987656899989547994989987798779893987654679459543478943348998923
8998987997999995987632456789545698789498545689999759213498986649899754597643812398656569651267899434
7999896889998754698743458997324789579597934778923978904597665431998996997532101259769778953456897645
6798765567899643987654599765437994468986724567910989895689543210127789876543312345978989764567979967
4987654378989756998765689976546792399654212389891296789798787631435678987654443656989999875678967898
2098754235678999779876797987656910987543101245789945679899976543576899998765564778999932996789458929
3989976127889987667997896798767891987654424699896832398932987656688978989896685889568899869892349310
9868997346999875454598945699898932398765536789995431987893498767899769875959786893456789654901258921
8754398757997654323579436989989693999896645892189546976789999989923459974349897932365696543212347893
6543239898996543212467949876976579899987756943478969875656898694304598763201998921234789876423456789
5432123999987954323678998765432455689299897894567899764348976543212797654512989432345998765434567892
7521012999899876549789999897621234579109998985678998654237898754344598865623976543456899876545679921
8633439878799987899897899998510123678998789976899899893156789765998789998734989954987932987678789540
9864598765689998965956798679323234569876577897916789989345678989899893239845698899898921099989898432
9998679854345999654346689598764345779765466789434598978976789998799932134956987645689542134599987656
8998798943239876543235595439895456989887345678949987956897899987679543235977898634579763289998799867
6899987654945998754123459910976567899996576789298976745679999896568954345989999548678975467896549878
5979898999899986543234567891297679999987897890197545236798998765467895456791987656799987579965434989
4656789889788998767349679999989989998799998989975432124567899865346896569942998979989998998943219898
3546798775667899898457889998967899989654349678985421013456792964236789998899879998879899987894698767
2124999654556999999578999887856789978993244569876534124567891098145698766679965987768798986999898756
4019886543445678998789498656745689869789123456998975335688989987234897655567894976543567895878986545
2129765432137999999894349743136789754698934567899996746899978976457986543456932987657678944669875436
7439876543446789987953219865347996543567897678985987968999767896569875432345891099768989323456996557
6545998655567896896432101965468987662378998989864699879987658899678987564676792943978993101245987698
9656789986878995987643229877599299831499349899943012989976545798989898665687899891099765622456798789
8967893297889689998784346987689123910976456789652127899965434567898799897798989799999865436567899893
7898989098993568999896598998891019899897887899754335789854323459998678998929978677892987545678978932
6989878999012456789998799989989129798789998979896467899765401568892489789219865566991297656789769991
5978567896434568999999988664578998654698999567987568987654312456791336678997654455789998769893456989
4564456789565689989899977553467976553487893459998679598776534587890124569898653234567899899932499879
3412367998798799976799765432149875432346892467899795439987659698989235879679765165689998989543989967
2101267899899899865789979574235996543556921235679899920398878969778956989469981012357897678999867898
3213458902942998654799988765346987965698910126896998891239989754567897892398792123968958569987658919
4323667899431299865678999875467899879899991298945987789645997643456789954999653246799542459876549201
6454798988949987978789543987598923989987789989539976567959865432347899999898754757898931345998678912
7565899567898765989999432399999212399876569878998765458967987541767979889769875768967890156999789323
8787893458999994395678940998798993569765498567897654378998998652378967678943986779756799299899996547
9898984567899989234567899895576789679854376456899989234989976543578954587892197889549898986789769756
8999765698969878987679999784445689798765212345698762134567897665689543456789098994338987654678959897
7689878789757767898991987643234778949854323456798743457789998778789642345678999543227699865789244998
6567989897643456789210299753124569534999954567987654568899899899999765467889987654103456986990123469
5478999976532345689321398921015678965987895678998865679945799998999876989996798793212568998932645567
4369898764321234789932987932124789976996799799879978795634568987899987896765979984324579679943787699
5456798765410123567893996893245899989875778986565989894325689976799898965434769876458689557899998789
6568899879321235778999875789359989898764567897454598965434798765689769996323459987568789445678999897
7679932998932446799987754568998766789832456779213457896545679874579943987899968998789893234789678965
8998921987999967899876543479899854698901234568902345679656789863467892399998799659899932145796567973
9867899896987899998765432456789783987892345689214696898767998654567999457987688943998643466965459892
8756798765695678999897551235699542356789466895438789999879979865678978969896567891239894997896569789
4348898654254768999959652357898421368996598987549899989999867979789767998765456789349989789998879675
2123998763123456789239743568987210156987679297656789878987656899897659798765345695498878678899998423
3239129864346579890126987679875431234898789198987997569876543988987545679892129989697666567789397674
5498998765457678989345999893986532456789999999798998678998732567896534569989398879986553435679298786
6987689986678799778999878912976543467895667894569989799998643488965423498878987969865432324778999897
9876579997989891566789967109898654567973456789994679987987656567893212987569976656986541013567899998
9765468998999932345698754398769767678962347999873598976598767779984329765467965545987656723459999999
7654357899989993996789965987659898889653458998762987654349898899876498654389894321299867936578987899
6543246789879879789898996986543969999784567897654998743236999999987569865234789210156978547789476789
6432135678967865678987989987542355678996788969879876574135789987598997973123579931345899658892345699
7687548789656654599876767895431234567899899356998765431013892393469986521034567892456789867901557789
9798656899545433987654456987650125678967943249659896432354989989578987432145679954667898979212678994
9898787998621012976512345798761334789458954998949999563765679878999976565358789976898997989954569313
8999998999732135997433558999873547997378999876797698754876798767989989987467898989929876896796798901
7899899986543249876547667898984756895489987945679549865677899654878990298998957599949765765689997899
6789763298854658997659879987899867896999876435678939978788998743456891999979546467899854234567895998
5678943149765867899792989976789998979899987549899598989899239654789979799868932345679972123478924987
4789432027986878998910996765678999456789898956965467899999198765689565689657896566798763244569109876
9899843456797899987899875654567892345798779767895345678988989876789434798798997678987654559789299954
9999754568998998976789654563479954467997659878943234589967979987996545899899698789899765667899987832
9998765678919987565699943242348976778976543989542199789856567699397696799943569898759876779999876521
8989876789109875454689893101567898989975432397659988998743434569298787898759699999643998896598765430
7976989893298755323456789232456789090986941098798767997632129678969899969898988998932449987439875421
6895492994398943212345795467767892191999892149899656789547298989654993456987776897891234598321987632
5999910989987653202456789578978943989898789236987646797656956996532389767996545456910123987532398943
4687899875499854578569897989989659878767679345998334689899897898675478979885432345891435798543459654
3496789986799875989689965395699899767654578959886123899989799999896567898765421256789545899866599975
2345678997898976798798754234579987654323467998765335789975639878987679999854210168998766789978789989
1456789998966987899898655123678999843212345689976547999764526567998989988975432259789987893199899899
0267899869445698999989632016789654954301386797897678998653212349999899867997546345678998992098965789
9469923954236789998679542124578979865716577896779989998632101239877645456789667456799659789987654567
8999919878997999997568993295689899876897679954567896987543212349765432347898778568892345694398767678
7887899989889999876457789989789789998998789343679964597654343598654321236789889678901299789459998789
6576789999768798765354699979998679789659893212489873298785764789965446545899998799999987999567999893
5454678988657669989212989767897578679943954401678994569876989999876557656999029891987686898978998942
6213469976543457898929876456896434578894975626899789978989899997998969797898998932396545657899997653
7534578954332598997899954345965323456789876545679679899998789896989979898967987543987432346998789767
8945789764101239876799875697894212399899998666789456789989679765879989999656798654987621499887679988
9767897653214398785789976989976201987989109877891345679765578954567898989545659765699710987674567899
9878999764323987674567897978943219896579299989943456998674458943456967878931349889799831298543456789
6989789975439876563456999867894329785498989998657569876533267932678955467893456992987542975432545699
5497569876798765442499998758965498674356678939978998975420157893489543248954567891598659864321234568
4323456988999654321378959967976987543234568921989987654321345789599432139896689932398769765434656899
5435567899998766562457949878987898656045679210191099865432467897678945546789796546459879876545767899

@ -0,0 +1,43 @@
#lang br
(require racket/file rackunit)
(define lines (map string->list (file->lines "10.rktd")))
(define left-toks (list #\[ #\( #\{ #\<))
(define right-toks (list #\] #\) #\} #\>))
(define duos (map list left-toks right-toks))
(define (matched-pair? left right) (member (list left right) duos))
(define (parse tokens)
(with-handlers ([char? values])
(let loop ([tokens tokens][lefts null])
(cond
[(empty? tokens) lefts]
[(memv (car tokens) left-toks)
(loop (cdr tokens) (cons (car tokens) lefts))]
[(and (pair? lefts) (matched-pair? (car lefts) (car tokens)))
(loop (cdr tokens) (cdr lefts))]
[else (raise (car tokens))]))))
(define (corrupt? parsed-line)
(case parsed-line
[(#\)) 3]
[(#\]) 57]
[(#\}) 1197]
[(#\>) 25137]
[else #f]))
(check-equal? (apply + (filter-map corrupt? (map parse lines))) 366027)
(define (autocomplete-score parsed-line)
(for/fold ([total 0])
([c parsed-line])
(+ (* total 5) (case c
[(#\() 1]
[(#\[) 2]
[(#\{) 3]
[(#\<) 4]))))
(define autocomplete-scores
(map autocomplete-score (filter-not corrupt? (map parse lines))))
(check-equal? (list-ref (sort autocomplete-scores >) (floor (/ (length autocomplete-scores) 2))) 1118645287)

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

@ -0,0 +1,52 @@
#lang br
(require racket/file rackunit)
(define (make-octopodes)
(define octopodes (make-hasheqv))
(for* ([(line lidx) (in-indexed (file->lines "11.rktd"))]
[(depth cidx) (in-indexed line)])
(hash-set! octopodes (make-rectangular cidx lidx) (string->number (string depth))))
octopodes)
(define octopodes (make-octopodes))
(define (neighbors loc)
(remove loc
(for*/list ([rd '(-1 0 1)]
[id '(-1 0 1)])
(+ loc (make-rectangular rd id)))))
(define (increment! loc)
(cond
[(number? (hash-ref octopodes loc #false))
(define next-val
(match (modulo (add1 (hash-ref octopodes loc)) 10)
[0 'flashed]
[val val]))
(hash-set! octopodes loc next-val)
(when (eq? next-val 'flashed)
(for-each increment! (neighbors loc)))]))
(define (find-flashes-and-reset)
(for/list ([(loc val) (in-hash octopodes)]
#:when (eq? val 'flashed))
(hash-set! octopodes loc 0)
loc))
(define (part-1)
(for/fold ([flashes null]
#:result (length flashes))
([step (in-range 100)])
(for-each increment! (hash-keys octopodes))
(append (find-flashes-and-reset) flashes)))
(check-equal? (part-1) 1705)
(set! octopodes (make-octopodes))
(define (part-2)
(for/or ([step (in-naturals)])
(for-each increment! (hash-keys octopodes))
(and (= (length (find-flashes-and-reset)) (length (hash-keys octopodes))) (add1 step))))
(check-equal? (part-2) 265)

@ -0,0 +1,10 @@
3113284886
2851876144
2774664484
6715112578
7146272153
6256656367
3148666245
3857446528
7322422833
8152175168

@ -0,0 +1,38 @@
#lang br
(require racket/file sugar rackunit racket/set racket/bool)
(define links (for/set ([line (file->lines "12.rktd")])
(list->set (string-split line "-"))))
(define (is-downcase? loc) (equal? (string-downcase loc) loc))
(define (paths-from last-loc links [small-caves (set)])
(match last-loc
["end" '(("end"))]
[loc #:when (and small-caves (set-member? small-caves loc))
(define pruned-links
(for*/set ([previous-locs (in-value (set-remove small-caves loc))]
[link links]
#:when (set-empty? (set-intersect link previous-locs)))
link))
(paths-from last-loc pruned-links #false)]
[loc
(define next-lc-visited (cond
[(false? small-caves) #false]
[(and (is-downcase? loc) (not (equal? loc "start")))
(set-add small-caves loc)]
[else small-caves]))
(define links-with-loc (for/set ([link links]
#:when (set-member? link loc))
link))
(define remaining-links (if (or (equal? loc "start")
(and (is-downcase? loc) (not small-caves)))
(set-subtract links links-with-loc)
links))
(for*/list ([link links-with-loc]
[next-loc (in-value (set-first (set-remove link loc)))]
[path (paths-from next-loc remaining-links next-lc-visited)])
(cons loc path))]))
(check-equal? (length (paths-from "start" links #f)) 5576)
(check-equal? (length (paths-from "start" links)) 152837)

@ -0,0 +1,23 @@
QR-da
QR-end
QR-al
start-op
zh-iw
zh-start
da-PF
op-bj
iw-QR
end-HR
bj-PF
da-LY
op-PF
bj-iw
end-da
bj-zh
HR-iw
zh-op
zh-PF
HR-bj
start-PF
HR-da
QR-bj

@ -0,0 +1,40 @@
#lang br
(require racket/file sugar rackunit racket/set)
(define-values (point-strs fold-strs) (splitf-at (file->lines "13.rktd") non-empty-string?))
(define points
(for/seteqv ([str point-strs])
(apply make-rectangular (map string->number (string-split (string-replace str "," " "))))))
(define creases
(for/list ([str fold-strs]
#:when (non-empty-string? str))
(match (string-split str "=")
[(list "fold along y" numstr) (* +i (string->number numstr))]
[(list "fold along x" numstr) (string->number numstr)])))
(define (do-crease points [stop-at +inf.0])
(for/fold ([points points])
([crease creases]
[i (in-range stop-at)])
(for/seteqv ([pt points])
(define imag-diff (- (imag-part pt) (imag-part crease)))
(define real-diff (- (real-part pt) (real-part crease)))
(cond
[(and (zero? (real-part crease)) (positive? imag-diff))
(+ (real-part pt) (* +i (- (imag-part crease) imag-diff)))]
[(and (zero? (imag-part crease)) (positive? real-diff))
(+ (- (real-part crease) real-diff) (* +i (imag-part pt)))]
[else pt]))))
(check-equal? (set-count (do-crease points 1)) 706)
(define matrix (do-crease points))
(for-each displayln
(map string-join
(for/list ([row (add1 (apply max (map imag-part (set->list matrix))))])
(for/list ([col (add1 (apply max (map real-part (set->list matrix))))])
(if (set-member? matrix (make-rectangular col row)) "X" " ")))))
;; draws word LRFJBJEH

@ -0,0 +1,865 @@
1118,85
1064,690
560,670
68,446
769,668
88,219
120,255
925,879
112,512
1064,652
1046,233
902,417
523,33
1012,280
1210,135
410,142
689,497
698,211
981,259
465,610
689,621
1093,887
114,728
790,809
139,59
798,257
390,457
482,281
701,190
840,283
320,723
498,504
256,397
100,115
401,92
796,780
239,729
755,539
736,870
676,567
484,560
405,511
187,387
356,155
42,659
182,508
256,374
572,320
738,207
328,80
70,528
1111,752
648,54
33,308
716,704
701,798
700,324
10,260
1076,401
554,533
592,885
269,575
492,528
646,837
1134,268
1290,619
442,248
549,519
908,647
402,280
1081,428
768,575
1277,525
472,121
985,635
912,374
512,499
708,807
1054,558
305,856
401,756
8,53
1044,256
607,581
156,840
756,331
771,609
276,456
1268,842
1123,387
155,526
1125,380
612,52
864,772
572,126
1084,597
666,397
574,169
33,481
1098,280
468,215
370,3
639,204
1230,466
512,684
705,161
738,126
355,376
1096,736
1166,476
1036,135
912,782
947,32
636,50
623,250
1257,215
416,672
989,74
1300,852
776,659
1196,838
1064,304
252,471
994,410
459,33
567,361
489,159
1240,528
58,506
944,339
788,0
319,857
644,215
787,33
1198,767
798,843
920,233
760,411
1300,626
52,311
812,558
378,227
226,437
386,444
169,291
308,172
698,618
514,231
761,235
157,774
798,684
902,29
428,37
698,52
405,383
1104,256
1058,784
299,518
617,332
346,498
1178,89
1290,275
1196,166
293,425
895,567
1034,662
596,56
687,250
534,255
623,698
1309,719
92,347
226,297
736,253
497,155
736,528
776,780
223,42
465,732
900,53
493,411
909,838
542,512
385,659
537,590
1228,80
420,462
761,519
879,852
1277,693
908,838
890,324
1092,383
691,862
602,378
416,224
296,598
666,215
234,347
902,142
1101,253
581,362
316,567
28,255
929,824
117,77
848,515
701,704
1136,233
485,680
1233,511
788,357
246,304
500,505
1144,63
738,320
964,620
623,413
26,495
58,390
82,590
930,311
239,578
748,784
873,518
385,879
311,242
1036,666
1230,641
1032,224
80,325
1233,383
1290,171
31,413
1076,493
402,838
731,690
977,522
92,36
144,476
520,365
428,396
1196,390
100,149
818,752
142,637
21,882
295,61
631,435
731,204
676,584
68,448
748,544
560,224
1,719
683,152
490,590
909,373
517,568
967,690
1015,450
1092,175
729,532
676,248
912,858
1180,0
174,233
73,586
403,292
325,635
308,474
733,511
38,787
278,222
562,36
485,603
296,822
428,149
433,494
1240,120
136,175
1240,366
1240,774
485,214
73,745
668,364
1071,119
185,514
882,268
544,614
540,838
139,773
1005,236
156,280
1290,584
813,155
209,826
773,572
715,852
11,700
562,350
209,701
293,33
117,301
1248,710
343,690
274,359
1146,646
930,666
189,162
161,383
485,291
912,558
252,784
1237,745
35,32
105,235
760,483
62,821
977,372
366,591
221,848
120,108
991,857
909,381
355,518
316,791
445,583
422,710
510,508
52,516
349,236
277,201
276,662
676,758
502,455
353,844
415,316
28,404
498,558
554,107
1101,701
935,873
865,701
1071,327
497,409
756,626
1272,555
1118,851
1073,280
385,463
954,827
1048,845
402,59
602,807
577,511
234,509
642,880
97,428
705,226
73,532
1179,33
74,87
226,361
256,484
1154,280
989,887
982,696
977,74
566,51
480,45
398,72
142,283
848,851
957,844
390,233
999,242
366,339
776,255
1096,830
845,162
403,826
1078,264
274,666
622,506
70,366
64,49
579,690
373,490
738,350
667,362
134,591
1064,808
438,212
53,215
226,533
359,416
989,603
1262,357
1096,64
701,544
890,570
358,296
821,175
851,33
356,826
1094,212
1213,428
596,390
1066,814
80,466
273,824
35,862
877,235
484,616
103,42
1181,810
385,435
768,736
1289,539
1173,568
760,847
826,726
808,631
714,388
274,135
900,752
808,263
810,57
468,887
38,824
907,826
1277,586
33,362
497,215
912,820
1054,397
512,658
64,497
410,814
689,301
756,533
1029,311
470,891
525,84
1002,474
540,248
664,501
912,72
70,8
353,162
1155,368
156,558
808,487
156,54
1300,260
192,67
33,273
338,266
909,92
117,814
1158,36
356,778
1258,516
1066,798
957,162
1228,768
361,469
157,215
810,505
687,413
335,37
1058,23
920,498
714,623
1282,137
1202,413
1076,122
251,327
310,469
1190,108
687,385
621,593
403,567
623,196
882,793
1228,590
408,477
296,72
114,248
808,256
469,828
825,456
718,9
1071,165
301,550
1169,882
1041,459
944,303
560,710
522,357
338,215
537,145
687,698
972,215
328,814
1265,404
321,74
909,597
1242,448
33,525
120,786
684,379
164,248
537,817
1158,401
329,259
349,210
1190,3
550,847
649,522
276,214
933,609
1248,73
542,158
842,332
842,108
726,366
1054,334
1168,645
252,871
681,792
602,29
1141,291
912,385
462,379
1174,495
633,38
20,310
1193,814
169,774
643,252
554,779
1002,722
1258,67
298,378
1054,547
502,858
1126,89
333,74
1014,520
1081,690
45,796
1081,466
146,469
909,166
902,752
1250,32
663,409
131,861
840,197
174,45
152,858
186,679
74,59
164,590
622,394
964,396
141,882
1153,663
584,8
909,728
321,7
1222,135
277,693
252,423
890,509
162,287
1071,266
110,627
305,38
1275,862
298,166
838,569
1054,484
493,595
110,491
381,455
572,687
70,120
773,817
229,204
566,135
18,50
114,390
565,26
902,466
1135,674
308,26
1210,485
502,493
944,555
256,560
1153,774
865,68
28,137
184,197
962,809
894,222
634,567
622,58
808,183
539,285
1277,397
522,537
112,158
1136,661
114,646
1200,280
1263,89
356,827
87,640
462,809
21,539
52,378
771,285
293,273
246,735
564,772
714,390
565,868
401,597
1034,438
621,621
1154,838
92,99
1300,186
1237,532
321,603
664,837
445,193
1092,831
234,401
100,745
1258,378
1198,127
550,411
1034,456
112,127
11,194
730,535
872,614
977,455
1174,719
937,190
950,836
237,280
661,344
246,204
1193,301
165,383
164,764
1009,102
994,268
373,190
1174,175
62,73
907,567
356,728
120,3
1252,56
209,641
113,226
821,595
738,277
1176,533
1009,498
687,196
1278,52
1179,861
502,519
1076,547
1169,12
882,37
244,814
661,550
925,659
1171,59
339,362
1076,285
1084,533
1210,745
808,519
764,749
689,273
1092,511
514,332
398,173
1014,72
401,138
1190,255
1250,862
622,388
687,478
333,372
370,190
694,385
1124,556
1056,749
649,792
154,729
301,844
329,635
58,838
940,190
281,641
515,387
813,215
841,262
617,556
1230,204
930,379
1012,291
1218,795
428,268
21,486
185,380
1029,583
315,586
281,583
100,268
1088,859
971,308
88,395
597,207
621,273
10,186
370,704
714,504
427,770
256,385
1218,544
485,829
59,318
744,135
160,607
972,716
440,736
291,862
20,584
840,891
294,500
920,457
776,639
679,435
276,227
1123,835
216,491
410,53
646,841
513,12
32,282
643,700
1158,455
1171,773
216,212
0,287
146,425
380,807
1138,57
932,227
555,539
21,42
346,396
688,282
385,431
398,273
428,857
132,313
540,646
52,827
161,859
1037,439
641,446
574,477
281,311
1198,158
1200,715
70,558
139,387
932,848
744,311
1002,172
964,26
169,120
301,498
642,364
580,731
909,354
217,7
1041,575
1289,38
1022,833
1279,481
1154,558
1196,504
104,607
415,809
1275,32
398,334
756,50
182,773
208,772
1154,728
1158,858
730,59
62,184
957,344
890,547
1176,591
73,362
515,507
1145,511
1277,621
62,9
1277,413
361,873
786,0
483,857
534,780
1252,838
796,332
1083,803
662,840
682,399
798,499
925,459
1228,687
895,316
541,276
208,324
523,705
907,292
468,332
1289,355
667,700
402,647
416,387
1054,560
31,481
920,396
234,45
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

@ -0,0 +1,54 @@
#lang br
(require racket/file sugar rackunit racket/dict)
(define lines (file->lines "14.rktd"))
(define (template->duos template-str)
(define cs (string->list template-str))
(for/list ([c1 cs]
[c2 (cdr cs)])
(list c1 c2)))
(define starting-template (car lines))
(define template (frequency-hash (template->duos starting-template)))
(define rule-strs (cddr lines))
(define procs (for/list ([rule-str rule-strs])
(match-define (list pat repl) (string-split rule-str " -> "))
(define duo (string->list pat))
(define replc (string-ref repl 0))
(λ (h)
(match (hash-ref h duo #f)
[#false null]
[duo-count
(define new-left-duo (list (first duo) replc))
(define new-right-duo (list replc (second duo)))
(cons
(cons duo (- duo-count))
(map (λ (new-duo) (cons new-duo duo-count))
(list new-left-duo new-right-duo)))]))))
(define (do-insertion template steps)
(for/fold ([template template])
([step steps])
(define delta-dict (append-map (λ (proc) (proc template)) procs))
(define this-template (make-hash))
(for ([(k v) (in-dict (append (hash->list template) delta-dict))])
(hash-update! this-template k (λ (val) (+ val v)) 0))
this-template))
(define (element-counts template)
(define orig-template-cs (string->list starting-template))
(define h (make-hash))
(for* ([(k v) (in-dict template)]
[c k])
(hash-update! h c (λ (val) (+ val v)) 0))
(for-each (λ (c) (hash-update! h c add1)) (list (first orig-template-cs) (last orig-template-cs)))
(for/hash ([(k v) (in-hash h)])
(values k (/ v 2))))
(define (score template)
(- (apply max (hash-values template)) (apply min (hash-values template))))
(check-equal? (score (element-counts (do-insertion template 10))) 2703)
(check-equal? (score (element-counts (do-insertion template 40))) 2984946368465)

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

@ -0,0 +1,59 @@
#lang br
(require racket/file graph rackunit)
(define lines (file->lines "15.rktd"))
(define grid (for*/list ([(line ridx) (in-indexed lines)]
[(num cidx) (in-indexed (map string->number (regexp-match* #rx"." line)))])
(cons (make-rectangular cidx ridx) num)))
(define data (make-hash grid))
(define (row-length data) (add1 (apply max (map imag-part (hash-keys data)))))
(define (col-length data) (add1 (apply max (map real-part (hash-keys data)))))
(define (make-grid-graph data)
(define rows (row-length data))
(define cols (col-length data))
(define g (weighted-graph/directed null))
(for ([k (in-hash-keys data)])
(add-vertex! g k))
(define (edges-between pt0 pt1)
(add-directed-edge! g pt0 pt1 (hash-ref data pt1))
(add-directed-edge! g pt1 pt0 (hash-ref data pt0)))
(for ([v (in-vertices g)])
(unless (= (real-part v) (sub1 cols))
(edges-between v (add1 v)))
(unless (= (imag-part v) (sub1 rows))
(edges-between v (+ +i v))))
g)
(define (preds->path preds dest)
(for/fold ([path (list dest)])
([i (in-naturals)]
#:break (zero? (car path)))
(cons (hash-ref preds (car path)) path)))
(define (solve data)
(define g (make-grid-graph data))
(define-values (paths preds) (dijkstra g 0))
(define dest (make-rectangular (sub1 (col-length data)) (sub1 (row-length data))))
(hash-ref paths dest))
(check-equal? (solve data) 487)
(define big-data (make-hash))
(define rows (row-length data))
(define cols (col-length data))
(for* ([(k v) (in-hash data)]
[rowrepeat 5]
[colrepeat 5])
(define next-val (+ v colrepeat rowrepeat))
(hash-set! big-data (+ k (make-rectangular (+ (* colrepeat cols)) (+ (* rowrepeat rows))))
(cond
[(> next-val 9) (- next-val 9)]
[else next-val])))
;; graph library is too slow to solve big grid
;; needs dynamic programming
#;(solve big-data)

@ -0,0 +1,100 @@
3576219475874583191916312133474175459337114195988185136398151631965391991813219974121211251194786128
1713881999231519357799114192443351147195293575386923868711388519361669464234397975938889146414199688
2151922113728581598379349983928489926261239862882888968998791929828316718921297117449631496888879522
3291722311299652548445221254456535112944431135116631433192483199481594768169444158222431199929579291
1472287315918113113959972189351931626141472991992791413146153989481318998519171231313898962992319251
8978798318873934543151823824117132228417869392811216547172919119998739911121199246294815175868992773
4711149989992717728465321161611967173674239298593935117599173279267114155265393397571372124856718732
9843245319692998612781837729915931371619886371128818266322121623171343132131239399743799827972585645
7183211712173272827112268971459424836293613294388761928991542137194981939169315879277494581991465822
2385721996578834579224651431411717198341139751429156637229888849181154922897746418293184413519931124
6188313732929496115412842928273812153715747411177189187726751991191841181119822534511125656211991265
8797112815141319881991983914861912838966446818811271274141461487947171138945321912192198815813211761
6131514331472951494911549373681638637191295997625615885219668259269297171939941498581919718223529141
3916711951831246219991217199412116783411218814949692281987659123812288937983969116847986196936523779
9111121227192813382983442234171148191131821497435387936912532695293912972491131977272231919798477344
9994893855122115213919995636978443773161196511775674783979472217917498285423121574533491643891265199
9245818921321877317663254739619499915821162412924816263648913499811482474392667993739139899758919393
1717618849193152316325119128859116191132672215933282127611134982961918179152189411131259943819997123
3697848795188715264992962617223177221118919863975198881197837936258983111819488622362584888223331999
1798259211962116675922944295117391732295963342131192112612386194749389991199821195162975124619149837
5365696872959269494682128272373619348283964191994187749824841195582221478325111187843285531119934786
6717218734155417511564162235845524121493736962442884479191718924594119911521121422231982184671122116
9346412964321187889663345611132183519588187791111227269112129947122999176114184938494824342939375892
2912811929919891428891223818529115949522464779949526791515797886471246181118183121786524127979998512
7353285281129811915971956929718433284441492484117162562212973157265812816731843791838571691421923924
3239161911151141369392413114665193927469988999219478935992541193184945926828911597158489999651151172
9229716159183212219791136121569512978927999177196111291668152292896495877722611129462617229299962112
5994461796358342348216915883991574569784887978731368749888449599246152414391635925763156831839199496
1569319989929452917219726368583887924179117119284971126181346919789949393972171262535283921926293357
9983673949614595396426594299567925779229751982433631919159114841538978735893759492592481573495963491
3725632421623191323789991288299296415942162988289929992238877153268488127718372211951117951486111926
1219319492614741819142363452974731825929485665978219725131218962626179997611254524524915768686381164
8248145978417221154822663831966124539168641311913211295941167919583189792718599334616827916567313217
3483959252172951953519211189255336234459522397526829492685113849198611624111948187714151963198671919
7812289559812455122297839958719917624841136314322613942214613249662544617928521344873779598816941946
6868958791119815122313931178313637489923479492171741443993619161318781194421239654468391996636923611
6999166111129212624151555925991311538952878946191234511973499736237742491811348911419449161895978178
3532973784281182181552481928144111246711742443831184236962981395142863562121961323169398316921741321
8433727191521455193223914171391447439164791659118411391327413285243717794299999523811137619197999791
8169125649963232439519311131691539694589231314268683691811431259295132344195953951287639889737849189
8928161919934761971442719391119881113318323987227229831917897151155121657624142152884624176461913498
2949199492196424164751892146395174669111921799261527568751283712133148212821261936297739912939743599
9931616797885267969938591912672996183187926259991298429278229232257219428413174972999193796827459799
3138768154311742899419449741617489465993174645228814938145273266941566223933119891379181296548999222
1338961668848636157444719898575174699419997972125194827176897565699991611654214368232592793825617931
9283643143692917314535119897889711191946688911436965194232811624712861576291987716317193114445363435
8718611398299869991788113111214414269554542114329621286592313532782819131919527228417764279755899812
8128249329314753847725634378737239939769913811444529224619715471961999278915341955188871714271145449
1286217894699991715153139268991918988338684391252271254127193342854156438873836258484975151167996316
4191439845728418348368191516746499654147257851256861534634988281199187541199892317145428248531129171
9799283477747387253566811199414256363339122372629545249542373591951156699415998714124115397811359516
3191985767689943186838293175311429527499612824265518522881922917119891424717751985621941995373481629
8127933395192162886259956635985821651844893796541627799276258692298972881471991789861232424926758792
4349683335349673317521761437148933192989961489478395417258212949865876242843242464123471244631449998
3983861771861891251448475182984833811718959521263444111998834975935221115813839514917176912994677265
3361812177831144971531441184671112122322312961859915722393944269211333951428175372918136112814926411
2492891241191991149319731182431211211971267682931551534199328118919751416881458658314218331565282229
1316191719349114251772629411233483317516143684488291792691839198723174287211241498955137331159443961
1295632882814319179937625629218199381191127966181781921829819969815912382191294796979515315471941392
1952318366571121641614137937116326339638859628226299126699118638171813164397332391891669798271559135
1123676641998146639499323488991286491444174358479157916696196841912233773269986812299996975117612192
2496461124219121332118311532239527696198787778122345883331631119147159212262659717289674149798169111
3994915157217763242581922474512992479146377117411981999549977495462427199524334285127723311599337186
2856249932899117429471617195415229923432185189218399415357391954728651191591964713418532917111296399
1916415314958527255299763241981929186681257596912948977397875248189748655173819381269954513499896991
4214987991254389723346911149661118597277889213928551947599177915888233175182921923128853579525754718
3582279578128192231339943295188563998645998149526284997789731787116991199391984141983177825883111821
3318115949152512995142691231324793411691198458654473675135993122216324137859959272731599996923465431
9431374329994955614816239978928993493129129641248882311462522524447519388869238199852941551532223632
6749132793518479891929891686579791123712167784913331559922969149125915827657111724348929978159964476
6899888192198113996189996152913216258469182711411338723293823541574149295918323829819122941288693461
9833728958423343229397292861582211971713299293611778961979819175799893793971822162383179128129741328
9719459171294372288418126215914532995125999274359633129923191213332337513812761149562146497198614329
6323381739518574154814713691189651618965915153972197836392492518147816118266818195856772641337997448
9515391698994467556612815425651153225477392335219196697477512338938131933398177519117437353271396315
2881769116189531115832167881372917543267529333184621316918481786111252281738298936313393416198972192
1262821848371618529985323295192217356152412696271318793198457225899782896767339942896611231999996177
7611115824284211421951282951346181221891123993118231772195952872865982323819415318945742389969468591
6289176957971112617212793915412958239574931721216722823933461763146195116737161463663388274118352919
2739192179312181318764617662499189345995579913463399176128133623751919135855775349195561978131664671
7419179135228196296753972713643556671138381494838116814212892453118963456121883515171981991493112197
2351962484381996196119791327953881225881124837211622273581789216339733276411173141146816794919111532
4828494193941149581788515219884312818182198261739598611663311138255885557141492174934279519487959315
6266441934234532987821537914696156313934795144412166199339199381197916324959558778257894958593689511
1218836684761811498991162549547375891999161535133511817388897491266957819219117988952234747197245136
7714718988198712181711441996373181351671962319416917816121119483959397254815953421521642116185224622
6315878819598412161782318179586237893968466116199183185764132999388411414821117761121199725212571499
9298381112525419214597534611761811137687414291394795245955115116482261788724982532573616634235949295
2513837618149681781571514782119218925111312739891194994857915371836413112952729282168681511999132471
9859244419394114469927618849921646911349163815889492121537711577378978633345419421881312649299234749
6241913851991287776811271361777919519213726771122769111499925516752149976397933753482495291399144132
7549785323789821947513966787125532113931855716249321712383292746672763573114656989299712174939322181
9188852123922512181114117536955617335173328988385462159798591117758397298294919117614163738414981949
2974419341361292817129453536824618155299993142149899966226312193848695911943416589913541194129913422
1672761999113289962322634642122163368424955339864962951462929691975798622972131599198673671183449929
3462285147627397867977869242451116812198183328383813959284449969199819896174618466572298882951459875
6153638634925613615423256118159531429294172496996993184762972841759153392753653191942715379879852345
6516291128536155113712481272992636373774429965994966353494115221979925592128929821611928492143929534
3993993153713717844719492314391992638719111242291783837279215399219822611221331985991484863975594721
5995891457939115884519762228128552168915218299831775259316392719113962218429351553267511523198149462