commit 6e31b7dd53e06bb6e06e5f717fbd538b4210aeb8 (patch)
parent 99505278327f387fc3e64392d46e32b3a23f9845
Author: Alex Karle <alex@alexkarle.com>
Date: Sun, 11 Dec 2022 12:59:06 -0500
day11: Add hacky part1 solution (no parsing)
Diffstat:
A | 2022/11/input | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2022/11/sample | | | 27 | +++++++++++++++++++++++++++ |
A | 2022/11/sol.scm | | | 88 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 170 insertions(+), 0 deletions(-)
diff --git a/2022/11/input b/2022/11/input
@@ -0,0 +1,55 @@
+Monkey 0:
+ Starting items: 83, 62, 93
+ Operation: new = old * 17
+ Test: divisible by 2
+ If true: throw to monkey 1
+ If false: throw to monkey 6
+
+Monkey 1:
+ Starting items: 90, 55
+ Operation: new = old + 1
+ Test: divisible by 17
+ If true: throw to monkey 6
+ If false: throw to monkey 3
+
+Monkey 2:
+ Starting items: 91, 78, 80, 97, 79, 88
+ Operation: new = old + 3
+ Test: divisible by 19
+ If true: throw to monkey 7
+ If false: throw to monkey 5
+
+Monkey 3:
+ Starting items: 64, 80, 83, 89, 59
+ Operation: new = old + 5
+ Test: divisible by 3
+ If true: throw to monkey 7
+ If false: throw to monkey 2
+
+Monkey 4:
+ Starting items: 98, 92, 99, 51
+ Operation: new = old * old
+ Test: divisible by 5
+ If true: throw to monkey 0
+ If false: throw to monkey 1
+
+Monkey 5:
+ Starting items: 68, 57, 95, 85, 98, 75, 98, 75
+ Operation: new = old + 2
+ Test: divisible by 13
+ If true: throw to monkey 4
+ If false: throw to monkey 0
+
+Monkey 6:
+ Starting items: 74
+ Operation: new = old + 4
+ Test: divisible by 7
+ If true: throw to monkey 3
+ If false: throw to monkey 2
+
+Monkey 7:
+ Starting items: 68, 64, 60, 68, 87, 80, 82
+ Operation: new = old * 19
+ Test: divisible by 11
+ If true: throw to monkey 4
+ If false: throw to monkey 5
diff --git a/2022/11/sample b/2022/11/sample
@@ -0,0 +1,27 @@
+Monkey 0:
+ Starting items: 79, 98
+ Operation: new = old * 19
+ Test: divisible by 23
+ If true: throw to monkey 2
+ If false: throw to monkey 3
+
+Monkey 1:
+ Starting items: 54, 65, 75, 74
+ Operation: new = old + 6
+ Test: divisible by 19
+ If true: throw to monkey 2
+ If false: throw to monkey 0
+
+Monkey 2:
+ Starting items: 79, 60, 97
+ Operation: new = old * old
+ Test: divisible by 13
+ If true: throw to monkey 1
+ If false: throw to monkey 3
+
+Monkey 3:
+ Starting items: 74
+ Operation: new = old + 3
+ Test: divisible by 17
+ If true: throw to monkey 0
+ If false: throw to monkey 1
diff --git a/2022/11/sol.scm b/2022/11/sol.scm
@@ -0,0 +1,88 @@
+#!/usr/local/bin/chicken-csi -ss
+(import (chicken io)
+ (chicken sort)
+ regex)
+
+(define-record monkey items inspect test counter)
+
+;; TODO: parse these!
+(define sample (vector
+ (make-monkey '(79 98)
+ (lambda (o) (* o 19))
+ (lambda (x) (if (= 0 (modulo x 23)) 2 3))
+ 0)
+ (make-monkey '(54 65 75 74)
+ (lambda (o) (+ o 6))
+ (lambda (x) (if (= 0 (modulo x 19)) 2 0))
+ 0)
+ (make-monkey '(79 60 97)
+ (lambda (o) (* o o))
+ (lambda (x) (if (= 0 (modulo x 13)) 1 3))
+ 0)
+ (make-monkey '(74)
+ (lambda (o) (+ o 3))
+ (lambda (x) (if (= 0 (modulo x 17)) 0 1))
+ 0)))
+
+(define input (vector
+ (make-monkey '(83 62 93)
+ (lambda (o) (* 17 o))
+ (lambda (x) (if (= 0 (modulo x 2)) 1 6))
+ 0)
+ (make-monkey '(90 55)
+ (lambda (o) (+ 1 o))
+ (lambda (x) (if (= 0 (modulo x 17)) 6 3))
+ 0)
+ (make-monkey '(91 78 80 97 79 88)
+ (lambda (o) (+ 3 o))
+ (lambda (x) (if (= 0 (modulo x 19)) 7 5))
+ 0)
+ (make-monkey '(64 80 83 89 59)
+ (lambda (o) (+ 5 o))
+ (lambda (x) (if (= 0 (modulo x 3)) 7 2))
+ 0)
+ (make-monkey '(98 92 99 51)
+ (lambda (o) (* o o))
+ (lambda (x) (if (= 0 (modulo x 5)) 0 1))
+ 0)
+ (make-monkey '(68 57 95 85 98 75 98 75)
+ (lambda (o) (+ 2 o))
+ (lambda (x) (if (= 0 (modulo x 13)) 4 0))
+ 0)
+ (make-monkey '(74)
+ (lambda (o) (+ 4 o))
+ (lambda (x) (if (= 0 (modulo x 7)) 3 2))
+ 0)
+ (make-monkey '(68 64 60 68 87 80 82)
+ (lambda (o) (* o 19))
+ (lambda (x) (if (= 0 (modulo x 11)) 4 5))
+ 0)))
+
+
+(define (take-turn 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)))
+ (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
+
+(define (simul flock rounds)
+ (if (= rounds 0)
+ 'done
+ (begin
+ (for-each (lambda (m) (take-turn m flock)) (vector->list flock))
+ (simul flock (sub1 rounds)))))
+
+(define (main args)
+ (simul input 20)
+ (let ((sorted (sort (map monkey-counter (vector->list input)) >)))
+ (print (* (car sorted)
+ (cadr sorted)))))