Skip to content

Instantly share code, notes, and snippets.

@schmalz
Last active September 19, 2023 15:15
Show Gist options
  • Save schmalz/d03e4dfda9eb7c89a1c4a2870e9a4885 to your computer and use it in GitHub Desktop.
Save schmalz/d03e4dfda9eb7c89a1c4a2870e9a4885 to your computer and use it in GitHub Desktop.
Advent of Code 2015; Day 2.
(defun box-dimensions (box-string)
"Given the dimensions of a box as a string in the form 'XxYxZ', return a
sequence containing the dimensions as integers X, Y and Z, sorted in
ascending order."
(sort (mapcar #'parse-integer
(str:split "x" box-string))
#'<))
(defun paper-for-box (box)
"Given the dimensions of BOX as integers sorted in ascending order, return
the amount of paper required to wrap the box."
(destructuring-bind (x y z) box
(+ (* 3 x y)
(* 2 x z)
(* 2 y z))))
(defun ribbon-for-box (box)
"Given the dimensions of BOX as integers sorted in ascending order, return
the amount of ribbon required to decorate the box."
(destructuring-bind (x y z) box
(+ (* 2 x)
(* 2 y)
(* x y z))))
(defun paper-for-all-boxes (file)
"Return the total amount of paper required to wrap all the boxes whose
dimensions are in FILE."
(reduce #'(lambda (acc line)
(+ acc
(paper-for-box (box-dimensions line))))
(uiop:read-file-lines file)
:initial-value 0))
(defun ribbon-for-all-boxes (file)
"Return the total amount of ribbon required to decorate all the boxes whose
dimensions are in FILE."
(reduce #'(lambda (acc line)
(+ acc
(ribbon-for-box (box-dimensions line))))
(uiop:read-file-lines file)
:initial-value 0))
@schmalz
Copy link
Author

schmalz commented Sep 14, 2023

Uses cl-str for string splitting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment