summaryrefslogtreecommitdiff
path: root/i-was-told-there-would-be-no-math/iwttwbnm.scm
diff options
context:
space:
mode:
Diffstat (limited to 'i-was-told-there-would-be-no-math/iwttwbnm.scm')
-rw-r--r--i-was-told-there-would-be-no-math/iwttwbnm.scm51
1 files changed, 51 insertions, 0 deletions
diff --git a/i-was-told-there-would-be-no-math/iwttwbnm.scm b/i-was-told-there-would-be-no-math/iwttwbnm.scm
new file mode 100644
index 0000000..1497be0
--- /dev/null
+++ b/i-was-told-there-would-be-no-math/iwttwbnm.scm
@@ -0,0 +1,51 @@
+(define-module (iwttwbnm)
+ #:use-module (srfi srfi-1)
+ #:export (iwttwbnm
+ calculate-box
+ calculate-ribbon))
+
+(define (iwttwbnm str)
+ (let ((dimensions
+ (map (lambda (line)
+ (map string->number (string-split line #\x)))
+ (string-split
+ (string-trim-both str char-set:whitespace)
+ #\newline))))
+ (list (apply
+ + (map (lambda (x)
+ (apply calculate-box x))
+ dimensions))
+ (apply
+ + (map (lambda (x)
+ (apply calculate-ribbon x))
+ dimensions)))))
+
+(define (calculate-box l w h)
+ "Given a length L, width W, and
+height H, returns the result of applying
+the following formula:
+
+2*l*w + 2*w*h + 2*h*l + min(l*w, w*h, h*l)"
+ (let* ((lw (* l w))
+ (wh (* w h))
+ (hl (* h l))
+ (areas (list lw wh hl)))
+ (+ (apply + (map (lambda (x)
+ (* 2 x))
+ areas))
+ (apply min areas))))
+
+(define (calculate-ribbon l w h)
+ "Given a length L, width W, and
+height H, returns the result of applying
+the following formula:
+
+min(2*(l+w), 2(w*h), 2(h*l)) + l*w*h"
+ (let ((lpw (+ l w))
+ (wph (+ w h))
+ (hpl (+ h l)))
+ (+ (apply min (map (lambda (x)
+ (* 2 x))
+ (list lpw wph hpl)))
+ (apply * (list l w h)))))
+