commit 01c817ca9438e806c37f9e6ed1a458ba0067650e (patch)
parent 6e31b7dd53e06bb6e06e5f717fbd538b4210aeb8
Author: Alex Karle <alex@alexkarle.com>
Date: Sun, 11 Dec 2022 13:11:51 -0500
day11: Add hacky part 2 solution
The math is right, but the solution isn't very robust! (still no
parsing, etc)
Diffstat:
1 file changed, 31 insertions(+), 7 deletions(-)
diff --git a/2022/11/sol.scm b/2022/11/sol.scm
@@ -24,6 +24,10 @@
(lambda (x) (if (= 0 (modulo x 17)) 0 1))
0)))
+;; no need to even use (lcm) since all primes!
+(define sample-lcm (* 23 19 13 17))
+(define input-lcm (* 2 17 19 3 5 13 7 11))
+
(define input (vector
(make-monkey '(83 62 93)
(lambda (o) (* 17 o))
@@ -59,30 +63,50 @@
0)))
-(define (take-turn m flock)
+(define (take-turn-1 m flock)
+ (let ((items (monkey-items m)))
+ (if (null? items)
+ 'done
+ (let* ((new-val (floor (/ ((monkey-inspect m) (car items)) 3)))
+ (next-monkey-idx ((monkey-test m) new-val))
+ (next-monkey (vector-ref flock next-monkey-idx)))
+ (monkey-counter-set! m (add1 (monkey-counter m)))
+ (monkey-items-set! m (cdr items))
+ (monkey-items-set! next-monkey (append (monkey-items next-monkey)
+ (list new-val)))
+ (take-turn-1 m flock))))) ; recurse for next item
+
+
+;; What do monkeys REALLY care about? They care about the
+;; divisibility by their number. If monkeys 1, 2, and 3
+;; care about divisibility A B C, then (lcm A B C) is the
+;; highest number we care about. We can truncate there.
+(define (take-turn-2 m flock)
; (print (map monkey-items (vector->list flock)))
; (print (map monkey-counter (vector->list flock)))
(let ((items (monkey-items m)))
(if (null? items)
'done
- (let* ((new-val (floor (/ ((monkey-inspect m) (car items)) 3)))
+ (let* ((new-val (modulo ((monkey-inspect m) (car items)) input-lcm))
(next-monkey-idx ((monkey-test m) new-val))
(next-monkey (vector-ref flock next-monkey-idx)))
(monkey-counter-set! m (add1 (monkey-counter m)))
(monkey-items-set! m (cdr items))
(monkey-items-set! next-monkey (append (monkey-items next-monkey)
(list new-val)))
- (take-turn m flock))))) ; recurse for next item
+ (take-turn-2 m flock))))) ; recurse for next item
+
-(define (simul flock rounds)
+(define (simul flock turn-proc rounds)
(if (= rounds 0)
'done
(begin
- (for-each (lambda (m) (take-turn m flock)) (vector->list flock))
- (simul flock (sub1 rounds)))))
+ (for-each (lambda (m) (turn-proc m flock)) (vector->list flock))
+ (simul flock turn-proc (sub1 rounds)))))
(define (main args)
- (simul input 20)
+ (simul input take-turn-2 10000)
(let ((sorted (sort (map monkey-counter (vector->list input)) >)))
+ ; (print (map monkey-items (vector->list input)))
(print (* (car sorted)
(cadr sorted)))))