summaryrefslogtreecommitdiff
path: root/calorie-counting/cc.scm
diff options
context:
space:
mode:
Diffstat (limited to 'calorie-counting/cc.scm')
-rw-r--r--calorie-counting/cc.scm29
1 files changed, 29 insertions, 0 deletions
diff --git a/calorie-counting/cc.scm b/calorie-counting/cc.scm
new file mode 100644
index 0000000..40a33bc
--- /dev/null
+++ b/calorie-counting/cc.scm
@@ -0,0 +1,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)
+ #\?))