AoC/2015/days/02/wrapping_paper.lisp

43 lines
1.3 KiB
Common Lisp

;; Day 2 - Wrapping paper
;;
;; Part 1: How many totral square feet of wrapping paper should the elves order?
;;
;; ref. https://asdf.common-lisp.dev/uiop.html#index-split_002dstring
;;
(defun dimension (str)
"split the 'lxwxh' encoding of present dimensions into a list of integers"
(mapcar (lambda (str-number) (parse-integer str-number))
(uiop:split-string str :separator "x")))
(defun side-areas (l w h)
"areas of the three kinds of side of a cuboid"
(list (* l w) (* w h) (* h l)))
(defun paper-area (l w h)
"area of the whole present wrapping paper: the area of the lxwxh cuboid plus the smaller paper square"
(let ((areas (side-areas l w h)))
(let ((min-area (apply #'min areas)))
(let ((present-area
(reduce '+ (mapcar (lambda (area) (* 2 area)) areas))))
(+ present-area min-area)))))
(defun paper-area-dimension (dimension)
(paper-area (nth 0 dimension) (nth 1 dimension) (nth 2 dimension)))
#|
(paper-area 2 3 4) ;; 58
(paper-area 1 1 10) ;; 43
|#
(defparameter area-sum 0)
(with-open-file (in "./data/input")
(do ((line (read-line in nil)
(read-line in nil)))
((null line))
(setf area-sum (+ area-sum
(paper-area-dimension
(dimension line))))))
(print area-sum)