diff options
author | bd <bdunahu@operationnull.com> | 2024-06-09 23:09:57 -0600 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2024-06-09 23:09:57 -0600 |
commit | e29188413ec6c3243f7965b4408459a7517539b3 (patch) | |
tree | 401b7a300b7fe1df7cec5230ed503baac9d9074c /i-was-told-there-would-be-no-math/iwttwbnm.scm | |
parent | e61508e4764d69a4ea0073f419b89ff0a06e813e (diff) |
AoC 2015.2 p1+2
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.scm | 51 |
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))))) + |