File tree Expand file tree Collapse file tree 4 files changed +1059
-0
lines changed Expand file tree Collapse file tree 4 files changed +1059
-0
lines changed Original file line number Diff line number Diff line change
1
+ two1nine
2
+ eightwothree
3
+ abcone2threexyz
4
+ xtwone3four
5
+ 4nineeightseven2
6
+ zoneight234
7
+ 7pqrstsixteen
Original file line number Diff line number Diff line change
1
+ 1abc2
2
+ pqr3stu8vwx
3
+ a1b2c3d4e5f
4
+ treb7uchet
Original file line number Diff line number Diff line change
1
+ #lang racket/base
2
+
3
+ (define (get-decimal-digit s i)
4
+ (define c (string-ref s i))
5
+ (and (char-numeric? c)
6
+ (- (char->integer c)
7
+ (char->integer #\0 ))))
8
+
9
+ (define digit-rxs
10
+ (for/list ([s (in-list '(zero one two three four five six seven eight nine))])
11
+ (regexp (format "^~a " s))))
12
+
13
+ (define (get-spelled-out-digit s i)
14
+ (or
15
+ (for/first ([(rx n) (in-indexed (in-list digit-rxs))]
16
+ #:when (regexp-match? rx s i))
17
+ n)
18
+ (get-decimal-digit s i)))
19
+
20
+ (define (calibration-value s [get-digit get-decimal-digit])
21
+ (define-values (d0 d1)
22
+ (for/fold ([d0 #f ]
23
+ [d1 #f ])
24
+ ([i (in-range 0 (string-length s))])
25
+ (define digit (get-digit s i))
26
+ (values (or d0 digit)
27
+ (or digit d1))))
28
+ (+ (* d0 10 ) d1))
29
+
30
+ (define part1
31
+ (call-with-input-file "day01.txt "
32
+ (lambda (in)
33
+ (for/sum ([line (in-lines in)])
34
+ (calibration-value line)))))
35
+
36
+ (module+ test
37
+ (require rackunit)
38
+ (check-equal? part1 56042 ))
39
+
40
+ (define part2
41
+ (call-with-input-file "day01.txt "
42
+ (lambda (in)
43
+ (for/sum ([line (in-lines in)])
44
+ (calibration-value line get-spelled-out-digit)))))
45
+
46
+ (module+ test
47
+ (require rackunit)
48
+ (check-equal? part2 55358 ))
You can’t perform that action at this time.
0 commit comments