diff options
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/11/solver.el | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/AoC2022/11/solver.el b/AoC2022/11/solver.el index 397b911..6b0c436 100644 --- a/AoC2022/11/solver.el +++ b/AoC2022/11/solver.el @@ -24,6 +24,7 @@ id items worry-level + divisor throw-to) (defun solver-monkey-make (id items operation divisor true-target false-target) @@ -36,11 +37,8 @@ (monkey--create :id (string-to-number id) :items (nreverse (mapcar #'string-to-number (split-string items ", "))) - :worry-level (lambda (item) - (thread-first - (funcall op item (or operand item)) ;; value item - (floor 3))) ;; loose interest - + :worry-level (lambda (item) (funcall op item (or operand item))) ;; value item + :divisor div :throw-to (lambda (item) (thread-first item (mod div) ;; throw input @@ -69,7 +67,7 @@ (goto-char (point-min)) (cl-loop for monkey = (solver-parse-monkey-entry) while monkey collect monkey))) -(defun solver-monkey-rounds (monkeys rounds) +(defun solver-monkey-rounds (rounds relaxation &rest monkeys) (let ((monkey-work (make-vector (length monkeys) 0))) ;; play round (dotimes (_ rounds) @@ -78,23 +76,32 @@ (setf (monkey-items monkey) nil) (cl-incf (aref monkey-work (monkey-id monkey)) (length items)) (mapc (lambda (item) - (let* ((item-concern (funcall (monkey-worry-level monkey) item)) + (let* ((item-concern (funcall relaxation (funcall (monkey-worry-level monkey) item))) (target-monkey (funcall (monkey-throw-to monkey) item-concern))) (push item-concern (monkey-items (cl-find target-monkey monkeys :key #'monkey-id))))) items)) ;; (message "monkey %d - items: %s\n all: %S" (monkey-id monkey) (monkey-items monkey) ;; (mapcar #'monkey-items monkeys)) )) - (solver-monkey-business monkey-work))) + monkey-work)) + +(defun solver-constant-slice (monkeys) + (cons (lambda (x) (floor x 3)) monkeys)) + +(defun solver-monkey-field (monkeys) + (let ((prime-field (apply #'* (mapcar #'monkey-divisor monkeys)))) + (cons (lambda (x) (mod x prime-field)) monkeys))) -(= 10605 - (thread-first - "eg-in" - (solver-get-monkeys) - (solver-monkey-rounds 20))) +(defun solver (filename rounds relaxer) + (thread-last + filename + (solver-get-monkeys) + (funcall relaxer) + (apply #'solver-monkey-rounds rounds) + (solver-monkey-business))) -(= 50830 - (thread-first - "input" - (solver-get-monkeys) - (solver-monkey-rounds 20))) +(ert-deftest test-solver () + (should (= 10605 (solver "eg-in" 20 #'solver-constant-slice))) + (should (= 50830 (solver "input" 20 #'solver-constant-slice))) + (should (= 2713310158 (solver "eg-in" 10000 #'solver-monkey-field))) + (should (= 14399640002 (solver "input" 10000 #'solver-monkey-field)))) |