From e5913a0f7018db74e8eb85a19d41e76028e8528e Mon Sep 17 00:00:00 2001 From: Tristan Williams Date: Sun, 12 Jan 2025 11:24:01 -0500 Subject: Refactor 2015 Day 1 Part 1 a bit --- src/2015.02.01-first-attempt.lisp | 43 +++++++++++++++++++++++---------------- 1 file 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 @@ (defconstant +PUZZLE-INPUT-LIST+ (loop for sublist in +PUZZLE-INPUT-STRING-LIST+ - collect (mapcar #'parse-integer sublist)) + collect (sort (mapcar #'parse-integer sublist) #'>)) "A list of presents and their numerical dimensions. Represented as a list of sublists, with each sublist containing the 3 dimensions of a particular present.") @@ -72,30 +72,39 @@ dimensions of a particular present.") ;; feet of wrapping paper should they order? ;; --------------------------------------------------------------------- +(defun wrapping-paper-base (DIMENSIONS) + "Calculate the base amount of wrapping paper needed for a present with `DIMENSIONS'. +`DIMENSIONS' is assumed to be a list of 3 elements." + (apply #'+ (list (* 2 (first DIMENSIONS) (second DIMENSIONS)) + (* 2 (first DIMENSIONS) (third DIMENSIONS)) + (* 2 (second DIMENSIONS) (third DIMENSIONS))))) + +(assert (= 52 (wrapping-paper-base '(4 3 2)))) +(assert (= 42 (wrapping-paper-base '(10 1 1)))) + + +(defun wrapping-paper-extra (DIMENSIONS) + "Calculate the extra amount of wrapping paper needed for a present with `DIMENSIONS'. +`DIMENSIONS' is assumed to be a list of 3 elements sorted highest to lowest." + (apply #'* (cdr DIMENSIONS))) -(defun smallest-two (DIMENSIONS) - "Return the smallest 2 `DIMENSIONS', assuming there are 3." - (cdr (sort DIMENSIONS #'>))) (defun wrapping-paper-area (DIMENSIONS) - "Calculate the amount of paper needed for a present with `DIMENSIONS'. -`DIMENSIONS' is assumed to be a list with 3 elements representing the -length, width, and height of a particular present." - (+ (apply #'+ (list (* 2 (first DIMENSIONS) (second DIMENSIONS)) - (* 2 (first DIMENSIONS) (third DIMENSIONS)) - (* 2 (second DIMENSIONS) (third DIMENSIONS)))) - (apply #'* (smallest-two DIMENSIONS)))) - -;; Test single-present examples from problem text -(assert (= 58 (wrapping-paper-area '(2 3 4)))) -(assert (= 43 (wrapping-paper-area '(1 1 10)))) + "Calculate the total amount of paper needed for a present with `DIMENSIONS'. +`DIMENSIONS' is assumed to be a list of 3 elements." + (+ (wrapping-paper-base DIMENSIONS) + (wrapping-paper-extra DIMENSIONS))) + +(assert (= 58 (wrapping-paper-area '(4 3 2)))) +(assert (= 43 (wrapping-paper-area '(10 1 1)))) + (defun wrapping-paper-total (PRESENTS) "Calculate the total amount of wrapping paper needed for the entire list of `PRESENTS'. -`PRESENTS' is assumed to be a list of sublists of 3 -elements each, representing the dimensions of each present." +`PRESENTS' is assumed to be a list of sublists of 3 elements each." (apply #'+ (mapcar #'wrapping-paper-area PRESENTS))) + ;; Test correct answer (assert (= 1586300 (wrapping-paper-total +PUZZLE-INPUT-LIST+))) -- cgit v1.2.3