aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/11
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-11 16:15:09 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-11 16:15:09 +0100
commit2d954d7a6beebe97d0e1f3030c8a06d9c65e033e (patch)
treee28c58397df68aeb03a1f59493dcbf3e5d67e9d0 /AoC2022/11
parent010aa30876456f03ac85bcecdbb45581d149c0a3 (diff)
downloadscratch-2d954d7a6beebe97d0e1f3030c8a06d9c65e033e.tar.gz
scratch-2d954d7a6beebe97d0e1f3030c8a06d9c65e033e.tar.bz2
scratch-2d954d7a6beebe97d0e1f3030c8a06d9c65e033e.zip
[AoC2022] Elisp Day 11 part2
Diffstat (limited to 'AoC2022/11')
-rw-r--r--AoC2022/11/solver.el43
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))))