1.rkt 569 B

123456789101112131415161718192021222324252627
  1. #lang racket
  2. (require "../lib/utils.rkt")
  3. (define lines
  4. (call-with-input-file "input"
  5. (λ (fp)
  6. (get-lines fp))))
  7. (define (parse-data line)
  8. (map string->number (string-split line)))
  9. (define data (map parse-data lines))
  10. (define (diff lst)
  11. (let loop ((cur lst) (ret '()))
  12. (if (or (null? cur) (null? (cdr cur)))
  13. (reverse ret)
  14. (loop (cdr cur) (cons (- (cadr cur) (car cur)) ret)))))
  15. (define (predict lst)
  16. (if (andmap (λ (x) (= x (car lst))) lst)
  17. (car lst)
  18. (+ (last lst) (predict (diff lst)))))
  19. (apply + (map predict data))