Created
December 3, 2018 18:53
-
-
Save hazkaz/e13992619d74766d8103ca86864172a4 to your computer and use it in GitHub Desktop.
aoc19 day one part two - solution in lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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