Skip to content

Commit a65118b

Browse files
committed
add day 9
1 parent 4b9be5e commit a65118b

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-0
lines changed

day09-example.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 3 6 9 12 15
2+
1 3 6 10 15 21
3+
10 13 16 21 30 45

day09.rkt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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))

0 commit comments

Comments
 (0)