Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.56 KB | None | 0 0
  1. (defun masses-from-file (filepath)
  2.   (with-open-file (handle filepath)
  3.                   (loop for line = (read-line handle nil nil)
  4.                         while line
  5.                         collect (parse-integer line))))
  6.  
  7. (defun fuel-for-mass (n)
  8.   (- (floor (/ n 3))
  9.      2))
  10.  
  11. (defun total-fuel-for-mass (a b)
  12.   (cond
  13.    ((< (fuel-for-mass b) 0) 0)
  14.    (t (+ (+ a (fuel-for-mass b))
  15.          (total-fuel-for-mass 0 (fuel-for-mass b))))))
  16.  
  17. (write (reduce #'total-fuel-for-mass
  18.                (masses-from-file "../input")
  19.                :initial-value 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement