day 11 part 2, yay

This commit is contained in:
efim 2022-12-13 06:29:13 +00:00
parent b4f8e937c7
commit f612d15eed
2 changed files with 117 additions and 12 deletions

View File

@ -172,7 +172,8 @@
(defparameter *test-monkey-instance*
(make-instance 'monkey
:test (lambda (num) (if (= num 4) 9 8)) :operation (lambda (num) (+ num 1))
:inventory '(100 60 30) :number 4))
:inventory '(100 60 30) :number 4
:mod 5))
(monkey-one-item-action *test-monkey-instance*)
(inspection-counter *test-monkey-instance*)
@ -222,3 +223,51 @@
;; now. if i read in all those "divisible by" so. what i want is to preserve MOD x
;; so, could get lowest common multiple and get mod from that? and set it in all monkeys,
;; or better - define it as class \ static attribute
(defparameter *11-input-paragraphs*
(cl-ppcre:split "\\n\\n" (uiop:read-file-string "day11-input.txt")))
(defparameter *11-input-structs* nil)
(defparameter *11-input-structs*
(coerce (mapcar #'monkey-struct-to-instance
(mapcar #'line-to-sexp *11-input-paragraphs*)) 'vector))
(loop
for monkey across *11-input-structs*
do (when (not (= 0 (mod (mod-reductor-2 monkey) (monkey-mod monkey))))
(setf (slot-value monkey 'mod-reductor-2) (* (mod-reductor-2 monkey) (monkey-mod monkey)))))
(mod 1 3)
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
;; yep.
;; now need to take mod by this one? and then it will preserve mod by any of previous, right?
(mod (+ 9699690 177) 5)
(mod 177 5)
;; now substitute my worry by mod over "common multiple" and run 20k cycles
(mod 4 7)
(progn
(defparameter *11-test-input* (cl-ppcre:split "\\n\\n" (uiop:read-file-string "day11-test.txt")))
(defparameter *11-test-structs*
(coerce (mapcar #'monkey-struct-to-instance
(mapcar #'line-to-sexp *11-test-input*)) 'vector))
;; oh, i need to reset and recalculate the lowest common multiple for test input.
;; yup
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
(loop
for monkey across *11-test-structs*
do (when (not (= 0 (mod (mod-reductor-2 monkey) (monkey-mod monkey))))
(setf (slot-value monkey 'mod-reductor-2) (* (mod-reductor-2 monkey) (monkey-mod monkey)))))
(loop
for i from 1 to 10000
do (progn (monkeys-round *11-test-structs*)
(format t "turn ~a~%" i)))
(print (apply #'* (subseq (sort
(mapcar #'inspection-counter
(coerce *11-test-structs* 'list)) #'>) 0 2)))
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1))
;; yeah

View File

@ -28,15 +28,21 @@
(inventory :accessor inventory :initarg :inventory)
(operation :reader operation :initarg :operation)
(test :reader test :initarg :test)
(inspection-counter :reader inspection-counter :initform 0)))
(inspection-counter :reader inspection-counter :initform 0)
(monkey-mod :reader monkey-mod :initarg :mod)
(mod-reductor-2 :reader mod-reductor-2 :initform 1 :allocation :class)))
(defmethod print-object ((obj monkey) stream)
(defmethod print-object ((obj monkey)
stream)
(print-unreadable-object (obj stream :type t)
(with-accessors ((order-number order-number )
(inventory inventory)
(inspection-counter inspection-counter))
(inspection-counter inspection-counter)
(monkey-mod monkey-mod)
(mod-reductor-2 mod-reductor-2))
obj
(format stream "~a, with objects: ~a; count: ~a" order-number inventory inspection-counter))))
(format stream "~a, with objects: ~a; count: ~a; mod: ~a; common: ~a"
order-number inventory inspection-counter monkey-mod mod-reductor-2))))
(defun new-from-old-function (operation operand1 operand2)
(eval `(lambda (old)
@ -58,7 +64,8 @@
false-monkey-number))))
(make-instance 'monkey
:test test-fun :operation operation-fun
:inventory inventory-list :number ordering-number))))
:inventory inventory-list :number ordering-number
:mod divisible-number))))
(defun monkey-one-item-action (monkey)
(let ((item-worry (first (inventory monkey) )))
@ -66,6 +73,7 @@
(setf (inventory monkey) (cdr (inventory monkey)))
(setq item-worry (funcall (operation monkey) item-worry))
;; (setq item-worry (floor (/ item-worry 3)))
(setq item-worry (mod item-worry (mod-reductor-2 monkey)))
(incf (slot-value monkey 'inspection-counter))
;; returning (target-monkey-num thrown-item)
(list (funcall (test monkey) item-worry) item-worry))))
@ -96,7 +104,6 @@
(progn
(defparameter *11-test-structs*
(coerce (mapcar #'monkey-struct-to-instance (mapcar #'line-to-sexp *11-test-input*)) 'vector))
(monkeys-round *11-test-structs*)
(loop
for i from 1 to 20
@ -113,11 +120,60 @@
(coerce (mapcar #'monkey-struct-to-instance (mapcar #'line-to-sexp *11-input-paragraphs*)) 'vector))
(loop
for i from 1 to 10000
for i from 1 to 20
do (progn (monkeys-round *11-input-structs*)
(format t "turn ~a~%" i)))
(apply #'* (subseq (sort (mapcar #'inspection-counter (coerce *11-input-structs* 'list)) #'>) 0 2)))
;;; PART 2.
(progn
(defparameter *11-test-input* (cl-ppcre:split "\\n\\n" (uiop:read-file-string "day11-test.txt")))
(defparameter *11-test-structs*
(coerce (mapcar #'monkey-struct-to-instance
(mapcar #'line-to-sexp *11-test-input*)) 'vector))
;; oh, i need to reset and recalculate the lowest common multiple for test input.
;; yup
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
(loop
for monkey across *11-test-structs*
do (when (not (= 0 (mod (mod-reductor-2 monkey) (monkey-mod monkey))))
(setf (slot-value monkey 'mod-reductor-2) (* (mod-reductor-2 monkey) (monkey-mod monkey)))))
(loop
for i from 1 to 10000
do (progn (monkeys-round *11-test-structs*)
(format t "turn ~a~%" i)))
(print (apply #'* (subseq (sort
(mapcar #'inspection-counter
(coerce *11-test-structs* 'list)) #'>) 0 2)))
;; that's the problem with sharing slots across class
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
)
(progn
(defparameter *11-input-input*
(cl-ppcre:split "\\n\\n" (uiop:read-file-string "day11-input.txt")))
(defparameter *11-input-structs*
(coerce (mapcar #'monkey-struct-to-instance
(mapcar #'line-to-sexp *11-input-input*)) 'vector))
;; oh, i need to reset and recalculate the lowest common multiple for test input.
;; yup
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
(loop
for monkey across *11-input-structs*
do (when (not (= 0 (mod (mod-reductor-2 monkey) (monkey-mod monkey))))
(setf (slot-value monkey 'mod-reductor-2) (* (mod-reductor-2 monkey) (monkey-mod monkey)))))
(loop
for i from 1 to 10000
do (progn (monkeys-round *11-input-structs*)
(format t "turn ~a~%" i)))
(print (apply #'* (subseq (sort
(mapcar #'inspection-counter
(coerce *11-input-structs* 'list)) #'>) 0 2)))
;; that's the problem with sharing slots across class
(setf (slot-value *test-monkey-instance* 'mod-reductor-2) 1)
)