Clisp 2015 day 2
This commit is contained in:
parent
55b4986f69
commit
7bc2c01242
72
common_lisp/2015/2/problem.lisp
Normal file
72
common_lisp/2015/2/problem.lisp
Normal file
@ -0,0 +1,72 @@
|
||||
(require :uiop)
|
||||
|
||||
(defun input-string () (string-trim '(#\newline) (uiop:read-file-string "../../../data/2015/2/input.txt")))
|
||||
|
||||
(defun input-string-list () (uiop:split-string (input-string) :separator (list #\linefeed)))
|
||||
|
||||
(defun string-to-dimension-list (string)
|
||||
(mapcar #'parse-integer
|
||||
(uiop:split-string string :separator (list #\x))))
|
||||
|
||||
(defun dimension-list-to-surface-area (dimension-list)
|
||||
(let ((length (first dimension-list))
|
||||
(width (second dimension-list))
|
||||
(height (third dimension-list)))
|
||||
(+ (* 2 length width) (* 2 length height) (* 2 width height))))
|
||||
|
||||
(defun dimension-list-to-slack (dimension-list)
|
||||
(let ((length (first dimension-list))
|
||||
(width (second dimension-list))
|
||||
(height (third dimension-list)))
|
||||
(let ((top (* length width))
|
||||
(front (* width height))
|
||||
(side (* length height)))
|
||||
(min top front side))))
|
||||
|
||||
(defun dimension-list-to-bow (dimension-list)
|
||||
(let ((length (first dimension-list))
|
||||
(width (second dimension-list))
|
||||
(height (third dimension-list)))
|
||||
(* length width height)))
|
||||
|
||||
(defun dimension-list-to-wrap-ribbon (dimension-list)
|
||||
(let ((length (first dimension-list))
|
||||
(width (second dimension-list))
|
||||
(height (third dimension-list)))
|
||||
(let ((top-perim (* 2 (+ length width)))
|
||||
(front-perim (* 2 (+ width height)))
|
||||
(side-perim (* 2 (+ length height))))
|
||||
(min top-perim front-perim side-perim))))
|
||||
|
||||
(defun dimension-list-to-ribbon (dimension-list)
|
||||
(+ (dimension-list-to-wrap-ribbon dimension-list) (dimension-list-to-bow dimension-list)))
|
||||
|
||||
(defun dimension-list-to-paper (dimension-list)
|
||||
(+ (dimension-list-to-surface-area dimension-list) (dimension-list-to-slack dimension-list)))
|
||||
|
||||
(defun string-to-paper (string)
|
||||
(dimension-list-to-paper (string-to-dimension-list string)))
|
||||
|
||||
(defun string-to-ribbon (string)
|
||||
(dimension-list-to-ribbon (string-to-dimension-list string)))
|
||||
|
||||
(defun part-1 ()
|
||||
(reduce (lambda (sfp string)
|
||||
(+ (string-to-paper string) sfp))
|
||||
(input-string-list)
|
||||
:initial-value 0))
|
||||
|
||||
(defun part-2 ()
|
||||
(reduce (lambda (sfp string)
|
||||
(+ (string-to-ribbon string) sfp))
|
||||
(input-string-list)
|
||||
:initial-value 0))
|
||||
|
||||
(defun solution ()
|
||||
(format t "Part 1: ~a" (part-1))
|
||||
(terpri)
|
||||
(format t "Part 2: ~a" (part-2))
|
||||
(terpri))
|
||||
|
||||
(solution)
|
||||
|
Loading…
Reference in New Issue
Block a user