(define-module (cc) #:use-module (ice-9 string-fun) #:export (cc sum-grouped-string sum-largest-n split-on-empty-line)) (define (cc str num) (sum-largest-n (map sum-grouped-string (split-on-empty-line str)) num)) (define (sum-grouped-string str) "Given STR, a newline delimited stream of numbers, returns their sum.." (apply + (map string->number (string-split str #\newline)))) (define (sum-largest-n lst num) "Given LST of numbers, sorts them and returns the top NUM amount." (apply + (list-head (sort lst >) num))) (define (split-on-empty-line str) (string-split (string-trim-both (string-replace-substring str "\n\n" "?") char-set:whitespace) #\?))