# HG changeset patch # User Steve Losh # Date 1575692606 18000 # Node ID 4febe2904f456ce7cb1915b7cee39f6b55a535a8 # Parent cb62272f4acc2d9fb13d8c777a59d0feac56b83b 2017/18 part 2 diff -r cb62272f4acc -r 4febe2904f45 src/2017/days/day-18.lisp --- a/src/2017/days/day-18.lisp Fri Dec 06 20:59:11 2019 -0500 +++ b/src/2017/days/day-18.lisp Fri Dec 06 23:23:26 2019 -0500 @@ -1,21 +1,13 @@ (defpackage :advent/2017/18 #.cl-user::*advent-use*) (in-package :advent/2017/18) -(defmacro opcase (op &body clauses) - (alexandria:once-only (op) - `(case (first ,op) - ,@(iterate - (for ((opcode . args) . body) :in clauses) - (collect `(,opcode (destructuring-bind ,args (rest ,op) - ,@body))))))) - (defclass* machine () ((id :type integer) (program :type vector) (pc :initform 0) (registers :initform (make-hash-table)) (queue :initform (make-queue)) - (waiting :initform nil))) + (blocked :initform nil))) (defun reg (machine register) (gethash register (registers machine) 0)) @@ -26,9 +18,17 @@ (defun val (machine register-or-constant) (etypecase register-or-constant - (symbol (r machine register-or-constant)) + (symbol (reg machine register-or-constant)) (number register-or-constant))) +(defmacro opcase (op &body clauses) + (alexandria:once-only (op) + `(case (first ,op) + ,@(iterate + (for ((opcode . args) . body) :in clauses) + (collect `(,opcode (destructuring-bind ,args (rest ,op) + ,@body))))))) + (defun handle-common-ops (machine op) (macrolet ((r (x) `(reg machine ,x)) (v (x) `(val machine ,x))) @@ -71,18 +71,17 @@ (counting (= 1 (id a))) (enqueue (val a x) (queue b)))) ((rcv x) (cond ((not (queue-empty-p (queue a))) - (setf (reg a x) (dequeue (queue a)))) - ((waiting b) (pr 'deadlock) (finish)) + (setf (reg a x) (dequeue (queue a)) + (blocked b) nil)) + ((blocked b) (pr 'deadlock) (finish)) (t (progn (decf (pc a)) - (setf (waiting a) t - (waiting b) nil) + (setf (blocked a) t) (rotatef a b)))))))) -(define-problem (2017 18) (data read-lines) (1187) +(define-problem (2017 18) (data read-lines) (1187 5969) (setf data (map 'vector #'read-all-from-string data)) (values (part1 data) - (part2 data)) - ) + (part2 data))) #; Scratch --------------------------------------------------------------------