2.rkt 980 B

123456789101112131415161718192021222324252627282930
  1. #lang racket
  2. (require "../lib/utils.rkt")
  3. (define fp (open-input-file "input"))
  4. (define lines (get-lines fp))
  5. (define (replace-digit str)
  6. (set! str (string-replace str "one" "o1ne"))
  7. (set! str (string-replace str "two" "t2wo"))
  8. (set! str (string-replace str "three" "t3hree"))
  9. (set! str (string-replace str "four" "f4our"))
  10. (set! str (string-replace str "five" "f5ive"))
  11. (set! str (string-replace str "six" "s6ix"))
  12. (set! str (string-replace str "seven" "s7even"))
  13. (set! str (string-replace str "eight" "e8ight"))
  14. (set! str (string-replace str "nine" "n9ine"))
  15. str)
  16. (define (extract-number rawline)
  17. (define line (replace-digit rawline))
  18. (define number-list (filter char-numeric? (string->list line)))
  19. (set! number-list (map (λ (c)
  20. (string->number (list->string (list c))))
  21. number-list))
  22. (+ (last number-list) (* 10 (car number-list))))
  23. (display (apply + (map extract-number lines)))
  24. (newline)