summaryrefslogtreecommitdiff
path: root/calorie-counting/cc.scm
blob: 40a33bc8a59789584ca6fd1e14b7b3e4e1e1638c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(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)
   #\?))