(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)))))