Skip to content

Commit 9a3da6d

Browse files
committed
add day 1
0 parents  commit 9a3da6d

File tree

4 files changed

+1059
-0
lines changed

4 files changed

+1059
-0
lines changed

day01-example-b.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
two1nine
2+
eightwothree
3+
abcone2threexyz
4+
xtwone3four
5+
4nineeightseven2
6+
zoneight234
7+
7pqrstsixteen

day01-example.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1abc2
2+
pqr3stu8vwx
3+
a1b2c3d4e5f
4+
treb7uchet

day01.rkt

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

0 commit comments

Comments
 (0)