diff --git a/aoc20.lisp b/aoc20.lisp index 09b9e88..7975f74 100644 --- a/aoc20.lisp +++ b/aoc20.lisp @@ -408,3 +408,45 @@ (p1 (solve-9-1 step-size data))) (print p1) (print (solve-9-2 data p1))))) + +;;; Day 10 + +(defun read-10 (file) + (read-9 file)) + +(defun solve-10-1 (data) + (let ((data (sort data #'<)) + (steps (list 1 0 1))) + (reduce (lambda (x y) + (let ((difference (- y x))) + (incf (nth (1- difference) steps)) + y)) + data) + (* (first steps) (third steps)))) + +(let ((ht (make-hash-table))) + (defun recurse-adapters (data) + (if (gethash (car data) ht) + (gethash (car data) ht) + (setf (gethash (car data) ht) + (%recurse-adapters data))))) + +(defun %recurse-adapters (data) + (if (null (cdr data)) + 1 + (let ((ceiling (+ 3 (first data)))) + (reduce #'+ (map 'list #'recurse-adapters + (remove-if-not + #'consp + (list (and (cdr data) (>= ceiling (cadr data)) (cdr data)) + (and (cddr data) (>= ceiling (caddr data)) (cddr data)) + (and (cdddr data) (>= ceiling (cadddr data)) (cdddr data))))))))) + +(defun solve-10-2 (data) + (let* ((data (cons 0 (coerce (sort data #'<) 'list)))) + (recurse-adapters data))) + +(defun solve-10 (file) + (let ((data (read-10 file))) + (time (progn (print (solve-10-1 data)) + (print (solve-10-2 data))))))