From 9d3f7ae1148f12e10c0179bdcc4992b44060f43a Mon Sep 17 00:00:00 2001
From: "Robert A. Uhl"
Date: Sun, 9 Dec 2018 22:57:00 -0500
Subject: [PATCH] Add day 9
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
I’m really proud of the fact that I thought about the problem,
implemented the solutions and got the correct answers, each time.
---
2018-12-09.lisp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
create mode 100644 2018-12-09.lisp
diff --git a/2018-12-09.lisp b/2018-12-09.lisp
new file mode 100644
index 0000000..689ab41
--- /dev/null
+++ b/2018-12-09.lisp
@@ -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))))
--
2.24.1