File tree Expand file tree Collapse file tree 3 files changed +245
-0
lines changed Expand file tree Collapse file tree 3 files changed +245
-0
lines changed Original file line number Diff line number Diff line change
1
+ 0 3 6 9 12 15
2
+ 1 3 6 10 15 21
3
+ 10 13 16 21 30 45
Original file line number Diff line number Diff line change
1
+ #lang racket/base
2
+
3
+ (require racket/list
4
+ racket/string)
5
+
6
+ (define (differences xs)
7
+ (for/list ([x (in-list xs)]
8
+ [y (in-list (cdr xs))])
9
+ (- y x)))
10
+
11
+ (define (history xs)
12
+ (let loop ([xs xs]
13
+ [ds null])
14
+ (if (andmap zero? xs)
15
+ (cons xs ds)
16
+ (loop
17
+ (differences xs)
18
+ (cons xs ds)))))
19
+
20
+ (define report
21
+ (call-with-input-file "day09.txt "
22
+ (lambda (in)
23
+ (for/list ([line (in-lines in)])
24
+ (map string->number (string-split line))))))
25
+
26
+ (define part1
27
+ (for/sum ([xs (in-list report)])
28
+ (for/sum ([ys (in-list (history xs))])
29
+ (if (null? ys) 0 (last ys)))))
30
+
31
+ (module+ test
32
+ (require rackunit)
33
+ (check-equal? part1 1757008019 ))
34
+
35
+ (define part2
36
+ (for/sum ([xs (in-list report)])
37
+ (for/fold ([res 0 ])
38
+ ([ys (in-list (history xs))])
39
+ (- (if (null? ys) 0 (car ys)) res))))
40
+
41
+ (module+ test
42
+ (check-equal? part2 995 ))
You can’t perform that action at this time.
0 commit comments