summaryrefslogtreecommitdiff
path: root/calorie-counting/cc.scm
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-06-10 18:13:57 -0600
committerbd <bdunahu@operationnull.com>2024-06-10 18:16:52 -0600
commit116aa074b190599e38a43bd6a8602a05cebdfb5d (patch)
tree179b2f48c84b243d0ad84dbe23ef7d813349d79b /calorie-counting/cc.scm
parent9e149cb6e6f1bf002fb1abc2b79c77afc3d1d36c (diff)
AoC 2022.1 p1+2
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)
+ #\?))