diff options
-rw-r--r-- | src/2015.02.01-first-attempt.lisp | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/2015.02.01-first-attempt.lisp b/src/2015.02.01-first-attempt.lisp index 14b3cfe..5a71a2b 100644 --- a/src/2015.02.01-first-attempt.lisp +++ b/src/2015.02.01-first-attempt.lisp | |||
@@ -36,7 +36,7 @@ | |||
36 | 36 | ||
37 | (defconstant +PUZZLE-INPUT-LIST+ | 37 | (defconstant +PUZZLE-INPUT-LIST+ |
38 | (loop for sublist in +PUZZLE-INPUT-STRING-LIST+ | 38 | (loop for sublist in +PUZZLE-INPUT-STRING-LIST+ |
39 | collect (mapcar #'parse-integer sublist)) | 39 | collect (sort (mapcar #'parse-integer sublist) #'>)) |
40 | "A list of presents and their numerical dimensions. | 40 | "A list of presents and their numerical dimensions. |
41 | Represented as a list of sublists, with each sublist containing the 3 | 41 | Represented as a list of sublists, with each sublist containing the 3 |
42 | dimensions of a particular present.") | 42 | dimensions of a particular present.") |
@@ -72,30 +72,39 @@ dimensions of a particular present.") | |||
72 | ;; feet of wrapping paper should they order? | 72 | ;; feet of wrapping paper should they order? |
73 | ;; --------------------------------------------------------------------- | 73 | ;; --------------------------------------------------------------------- |
74 | 74 | ||
75 | (defun wrapping-paper-base (DIMENSIONS) | ||
76 | "Calculate the base amount of wrapping paper needed for a present with `DIMENSIONS'. | ||
77 | `DIMENSIONS' is assumed to be a list of 3 elements." | ||
78 | (apply #'+ (list (* 2 (first DIMENSIONS) (second DIMENSIONS)) | ||
79 | (* 2 (first DIMENSIONS) (third DIMENSIONS)) | ||
80 | (* 2 (second DIMENSIONS) (third DIMENSIONS))))) | ||
81 | |||
82 | (assert (= 52 (wrapping-paper-base '(4 3 2)))) | ||
83 | (assert (= 42 (wrapping-paper-base '(10 1 1)))) | ||
84 | |||
85 | |||
86 | (defun wrapping-paper-extra (DIMENSIONS) | ||
87 | "Calculate the extra amount of wrapping paper needed for a present with `DIMENSIONS'. | ||
88 | `DIMENSIONS' is assumed to be a list of 3 elements sorted highest to lowest." | ||
89 | (apply #'* (cdr DIMENSIONS))) | ||
75 | 90 | ||
76 | (defun smallest-two (DIMENSIONS) | ||
77 | "Return the smallest 2 `DIMENSIONS', assuming there are 3." | ||
78 | (cdr (sort DIMENSIONS #'>))) | ||
79 | 91 | ||
80 | (defun wrapping-paper-area (DIMENSIONS) | 92 | (defun wrapping-paper-area (DIMENSIONS) |
81 | "Calculate the amount of paper needed for a present with `DIMENSIONS'. | 93 | "Calculate the total amount of paper needed for a present with `DIMENSIONS'. |
82 | `DIMENSIONS' is assumed to be a list with 3 elements representing the | 94 | `DIMENSIONS' is assumed to be a list of 3 elements." |
83 | length, width, and height of a particular present." | 95 | (+ (wrapping-paper-base DIMENSIONS) |
84 | (+ (apply #'+ (list (* 2 (first DIMENSIONS) (second DIMENSIONS)) | 96 | (wrapping-paper-extra DIMENSIONS))) |
85 | (* 2 (first DIMENSIONS) (third DIMENSIONS)) | 97 | |
86 | (* 2 (second DIMENSIONS) (third DIMENSIONS)))) | 98 | (assert (= 58 (wrapping-paper-area '(4 3 2)))) |
87 | (apply #'* (smallest-two DIMENSIONS)))) | 99 | (assert (= 43 (wrapping-paper-area '(10 1 1)))) |
88 | 100 | ||
89 | ;; Test single-present examples from problem text | ||
90 | (assert (= 58 (wrapping-paper-area '(2 3 4)))) | ||
91 | (assert (= 43 (wrapping-paper-area '(1 1 10)))) | ||
92 | 101 | ||
93 | (defun wrapping-paper-total (PRESENTS) | 102 | (defun wrapping-paper-total (PRESENTS) |
94 | "Calculate the total amount of wrapping paper needed for the entire list of `PRESENTS'. | 103 | "Calculate the total amount of wrapping paper needed for the entire list of `PRESENTS'. |
95 | `PRESENTS' is assumed to be a list of sublists of 3 | 104 | `PRESENTS' is assumed to be a list of sublists of 3 elements each." |
96 | elements each, representing the dimensions of each present." | ||
97 | (apply #'+ (mapcar #'wrapping-paper-area PRESENTS))) | 105 | (apply #'+ (mapcar #'wrapping-paper-area PRESENTS))) |
98 | 106 | ||
107 | |||
99 | ;; Test correct answer | 108 | ;; Test correct answer |
100 | (assert (= 1586300 (wrapping-paper-total +PUZZLE-INPUT-LIST+))) | 109 | (assert (= 1586300 (wrapping-paper-total +PUZZLE-INPUT-LIST+))) |
101 | 110 | ||