diff options
| author | Mistivia <i@mistivia.com> | 2024-02-14 14:55:21 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-02-14 14:55:21 +0800 |
| commit | dc6b5fe160647afcaff88242792113b5802176fe (patch) | |
| tree | fbaf92803820f820716ea584bd951267c9080a2e /advent-of-code/2023/01/part2.rkt | |
| parent | 60b0586ddc42cd8060e796e61267fc63a4397712 (diff) | |
refactor advent of code 2023 day 01 from c to racket
Diffstat (limited to 'advent-of-code/2023/01/part2.rkt')
| -rw-r--r-- | advent-of-code/2023/01/part2.rkt | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/advent-of-code/2023/01/part2.rkt b/advent-of-code/2023/01/part2.rkt new file mode 100644 index 0000000..85602da --- /dev/null +++ b/advent-of-code/2023/01/part2.rkt @@ -0,0 +1,36 @@ +#lang racket + +(define fp (open-input-file "input")) +(define sum 0) + +(define (get-lines fp) + (let loop ((lines '())) + (define l (read-line fp)) + (if (eof-object? l) + (reverse lines) + (loop (cons l lines))))) + +(define lines (get-lines fp)) + +(define (replace-digit str) + (set! str (string-replace str "one" "o1ne")) + (set! str (string-replace str "two" "t2wo")) + (set! str (string-replace str "three" "t3hree")) + (set! str (string-replace str "four" "f4our")) + (set! str (string-replace str "five" "f5ive")) + (set! str (string-replace str "six" "s6ix")) + (set! str (string-replace str "seven" "s7even")) + (set! str (string-replace str "eight" "e8ight")) + (set! str (string-replace str "nine" "n9ine")) + str) + +(define (extract-number rawline) + (define line (replace-digit rawline)) + (define number-list (filter char-numeric? (string->list line))) + (set! number-list (map (lambda (c) + (string->number (list->string (list c)))) + number-list)) + (+ (last number-list) (* 10 (car number-list)))) + +(display (apply + (map extract-number lines))) +(newline) |
