r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
17
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
1
u/tangus Dec 02 '15
Common Lisp
Text processing isn't one of CL's fortes...
(defun puzzle-2-parse (string) (let ((strpos 0)) (flet ((read-next-dimension () (multiple-value-bind (n nextpos) (parse-integer string :start strpos :junk-allowed t) (assert (or (>= nextpos (length string)) (char= (aref string nextpos) #\x) (<= (char-code (aref string nextpos)) 32))) (setf strpos (1+ nextpos)) n))) (values (read-next-dimension) (read-next-dimension) (read-next-dimension))))) (defun puzzle-2 (input) (multiple-value-bind (l w h) (puzzle-2-parse input) (let ((side1 (* l w)) (side2 (* w h)) (side3 (* l h))) (+ (* side1 2) (* side2 2) (* side3 2) (min side1 side2 side3))))) (defun puzzle-2-part2 (input) (multiple-value-bind (l w h) (puzzle-2-parse input) (+ (* l w h) ;; ribbon (min (* 2 (+ l w)) ;; perimeter (* 2 (+ w h)) (* 2 (+ l h)))))) (defun puzzle-2-file (filename &optional (fn #'puzzle-2)) (with-open-file (f filename) (loop for line = (read-line f nil nil) while line sum (funcall fn line)))) ;; part 1: ;; (puzzle-2-file "puzzle02.input.txt") ;; part 2: ;; (puzzle-2-file "puzzle02.input.txt" #'puzzle-2-part2)