master
Matthew Butterick 4 years ago
parent d7b8a8fe71
commit 1ae744093d

@ -0,0 +1,31 @@
#lang br
(require racket/file rackunit)
(define adapters (map string->number (file->lines "10.rktd")))
(define sources (sort `(0 ,@adapters ,(+ (apply max adapters) 3)) <))
(define diffs (for/list ([left sources]
[right (cdr sources)])
(- right left)))
(check-equal? (* (count (curry = 1) diffs) (count (curry = 3) diffs)) 2590)
(define (sequential-subsequences sources [seqs null])
(match sources
[(? null?) seqs]
[(list head tail ...)
(let loop ([head (list head)][tail tail])
(match tail
[(list first rest ...) #:when (eq? (car head) (sub1 first))
(loop (cons first head) rest)]
[_ (sequential-subsequences tail (cons head seqs))]))]))
(define (subseq->multiplier subseq)
(match (length subseq)
[3 2] ; abc | ac
[4 4] ; abcd | acd | abd | ad
[5 7] ; abcde | acde | abde | abce | abe | ace | ade
[_ 1]))
(check-equal? (for/product ([subseq (sequential-subsequences sources)])
(subseq->multiplier subseq)) 226775649501184)

@ -0,0 +1,108 @@
105
78
37
153
10
175
62
163
87
22
24
92
46
5
115
61
124
128
8
60
17
93
166
29
90
148
113
55
141
134
79
101
49
133
38
53
33
30
66
159
23
132
145
147
121
94
146
21
135
56
176
118
44
138
85
169
111
9
1
83
36
59
140
149
160
43
131
69
2
25
84
39
28
171
172
100
18
15
114
70
86
97
155
152
40
122
77
16
11
170
52
45
139
76
102
63
54
142
14
158
80
154
112
91
108
73
127
123