Skip to content

Instantly share code, notes, and snippets.

@hazkaz
Created December 3, 2018 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazkaz/e13992619d74766d8103ca86864172a4 to your computer and use it in GitHub Desktop.
Save hazkaz/e13992619d74766d8103ca86864172a4 to your computer and use it in GitHub Desktop.
aoc19 day one part two - solution in lisp
;; function to check if a given value exists in a list
(defun in (val list)
(if (eq (car list) val) T (if (eq (cdr list) NIL) NIL (in val (cdr list)))))
;; loop through the list in a wraparound fashion until a frequency repeats itself
(let (
(total 0)
(data (mapcar #'parse-integer (uiop:read-file-lines "1.input")))
(n 0)
(existing NIL)
) (loop for num = (nth n data)
while (not (in total existing)) do (progn
;; add the current frequency to the list 'existing'
(setf existing (cons total existing))
(setf total (+ total num))
(setf n (+ 1 n))
(setf n (mod n (list-length data)))))
;; print the total once done
(format t "~A~%" total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment