summaryrefslogtreecommitdiff
path: root/i-was-told-there-would-be-no-math/iwttwbnm.scm
blob: 1497be0dbb7415bfdfc6627f8dd4edb1e90d91ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)))))