9d3f7ae1148f12e10c0179bdcc4992b44060f43a — Robert A. Uhl 10 months ago 0200049
Add day 9

I’m really proud of the fact that I thought about the problem,
implemented the solutions and got the correct answers, each time.
1 files changed, 82 insertions(+), 0 deletions(-)

A 2018-12-09.lisp
A 2018-12-09.lisp => 2018-12-09.lisp +82 -0
@@ 0,0 1,82 @@
+ (defpackage "AOC/9" (:use "CL"))
+ (in-package "AOC/9")
+ 
+ ;; store marbles in a record (number . (prev . next))
+ (defun value (current)
+   (car current))
+ 
+ (defun prev (current)
+   (cadr current))
+ 
+ (defun (setf prev) (new-value current)
+   (setf (cadr current) new-value))
+ 
+ (defun next (current)
+   (cddr current))
+ 
+ (defun (setf next) (new-value current)
+   (setf (cddr current) new-value))
+ 
+ (defun print-marbles (current)
+   (cons (value current)
+         (loop for m = (next current) then (next m)
+               until (eq m current)
+               collect (value m))))
+ 
+ (defun insert (value current)
+   (let* ((a (next current))
+          (b (next a))
+          (new (cons value (cons a b))))
+     (setf (next a) new
+           (prev b) new)))
+ 
+ (defun start ()
+   (let ((node (cons 0 (cons nil nil))))
+     (setf (next node) node
+           (prev node) node)))
+ 
+ (defun score (value current)
+   (let* ((a (prev (prev (prev (prev (prev (prev current)))))))
+          (b (prev a))
+          (c (prev b)))
+     (setf (prev a) c
+           (next c) a)
+     (values a (+ value (value b)))))
+ 
+ (defun turn (value current)
+   (if (zerop (mod value 23))
+       (score value current)
+       (insert value current)))
+ 
+ ;; (defun game (players high-score)
+ ;;   (loop for i from 1
+ ;;         with c = (start)
+ ;;         and scores = (make-list players)
+ ;;         while (< (apply #'max scores) high-score)
+ ;;         do (multiple-value-bind (current score)
+ ;;                (turn i c)
+ ;;              (when score
+ ;;                (incf (nth (mod i players) scores) score))
+ ;;              (setf c current))))
+ 
+ (defun solution-1 ()
+   (loop for i from 1 to 71482
+         with c = (start)
+         and scores = (make-list 424 :initial-element 0)
+         do (multiple-value-bind (current score)
+                (turn i c)
+              (when score
+                (incf (nth (mod i 424) scores) score))
+              (setf c current))
+         finally (return (apply #'max scores))))
+ 
+ (defun solution-2 ()
+   (loop for i from 1 to 7148200
+         with c = (start)
+         and scores = (make-list 424 :initial-element 0)
+         do (multiple-value-bind (current score)
+                (turn i c)
+              (when score
+                (incf (nth (mod i 424) scores) score))
+              (setf c current))
+         finally (return (apply #'max scores))))