You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

;;; Day 1
(defun read-1 (file)
(with-open-file (stream file)
(loop for line = (read-line stream nil)
while line
collect (parse-integer line))))
(defun solve-1-1 (file)
(let ((readings (read-1 file))
(inc-acc 0))
(labels ((rec (list old)
(if list
(progn
(if (< old (first list))
(incf inc-acc))
(rec (cdr list) (first list))))))
(rec (cdr readings) (first readings))
inc-acc)))
(defun solve-1-2 (file)
(let ((readings (read-1 file))
(inc-acc 0))
(labels ((rec (list old-sum)
(if (cddr list)
(let ((sum (reduce #'+ (subseq list 0 3))))
(if (< old-sum sum)
(incf inc-acc))
(rec (cdr list) sum)))))
(rec (cdr readings) (reduce #'+ (subseq readings 0 3)))
inc-acc)))
;;; Day 2
(defun setup-solution-2 (stage)
(let ((position (vector 0 0))
(aim 0))
(defun forward (n)
(incf (elt position 0) n)
(if (= stage 2) (incf (elt position 1) (* n aim))))
(defun up (n)
(if (= stage 1)
(decf (elt position 1) n)
(decf aim n)))
(defun down (n)
(if (= stage 1)
(incf (elt position 1) n)
(incf aim n)))
(defun zero ()
(setf position (vector 0 0) aim 0))
(defun result ()
position)))
(defun read-2-1 (file)
(with-open-file (stream file)
(loop for line = (read-line stream nil)
while line
collect (multiple-value-bind (symbol length) (read-from-string line)
(list symbol (parse-integer (subseq line length)))))))
(defun solve-2-1 (data)
(setup-solution-2 1)
(eval (cons 'progn data))
(reduce #'* (result)))
(defun solve-2-2 (data)
(setup-solution-2 2)
(eval (cons 'progn data))
(reduce #'* (result)))