diff --git a/input/11.txt b/input/11.txt new file mode 100644 index 0000000..4a696d9 --- /dev/null +++ b/input/11.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 91, 66 + Operation: new = old * 13 + Test: divisible by 19 + If true: throw to monkey 6 + If false: throw to monkey 2 + +Monkey 1: + Starting items: 78, 97, 59 + Operation: new = old + 7 + Test: divisible by 5 + If true: throw to monkey 0 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 57, 59, 97, 84, 72, 83, 56, 76 + Operation: new = old + 6 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 3: + Starting items: 81, 78, 70, 58, 84 + Operation: new = old + 5 + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 0 + +Monkey 4: + Starting items: 60 + Operation: new = old + 8 + Test: divisible by 7 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 5: + Starting items: 57, 69, 63, 75, 62, 77, 72 + Operation: new = old * 5 + Test: divisible by 13 + If true: throw to monkey 7 + If false: throw to monkey 4 + +Monkey 6: + Starting items: 73, 66, 86, 79, 98, 87 + Operation: new = old * old + Test: divisible by 3 + If true: throw to monkey 5 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 95, 89, 63, 67 + Operation: new = old + 2 + Test: divisible by 2 + If true: throw to monkey 1 + If false: throw to monkey 4 diff --git a/lib.rkt b/lib.rkt index c378a11..1e365bb 100644 --- a/lib.rkt +++ b/lib.rkt @@ -212,6 +212,10 @@ ;; % : number -> number -> number (define % modulo) +;; divisible? : number -> number -> number +(define (divisible? n m) + (= (% n m) 0)) + ;; number->digits-reverse : number -> (listof number) ;; Return the digits of the given number in reverse order (i.e. RTL) (define (number->digits-reverse n) diff --git a/src/11.rkt b/src/11.rkt new file mode 100644 index 0000000..b1e5eeb --- /dev/null +++ b/src/11.rkt @@ -0,0 +1,52 @@ +#lang curly-fn racket + +(require "../lib.rkt") + +(struct monkey ([items #:mutable] op modulus t f [inspections #:mutable]) #:transparent) + +(define-syntax monkeys + (λ (stx) + (syntax-case stx () + [monkeys (syntax (vector (monkey '(91 66) #{* % 13} 19 6 2 0) + (monkey '(78 97 59) #{+ % 7} 5 0 3 0) + (monkey '(57 59 97 84 72 83 56 76) #{+ % 6} 11 5 7 0) + (monkey '(81 78 70 58 84) #{+ % 5} 17 6 0 0) + (monkey '(60) #{+ % 8} 7 1 3 0) + (monkey '(57 69 63 75 62 77 72) #{* % 5} 13 7 4 0) + (monkey '(73 66 86 79 98 87) #{* % %} 3 5 2 0) + (monkey '(95 89 63 67) #{+ % 2} 2 1 4 0)))]))) +(define monkeys1 monkeys) +(define monkeys2 monkeys) + +(define modulus (for/product ([monkey monkeys]) (monkey-modulus monkey))) + +(define (give! monkeys i item) + (define monkey (vector-ref monkeys i)) + (set-monkey-items! monkey (snoc (monkey-items monkey) item))) + +(define (round! monkeys f) + (for* ([monkey monkeys] + [item (monkey-items monkey)]) + (define item* (f ((monkey-op monkey) item))) + (if (divisible? item* (monkey-modulus monkey)) + (give! monkeys (monkey-t monkey) item*) + (give! monkeys (monkey-f monkey) item*)) + (set-monkey-inspections! monkey (+ (monkey-inspections monkey) + (length (monkey-items monkey)))) + (set-monkey-items! monkey '()))) + +(define (monkey-business monkeys) + (define is (sort (map monkey-inspections (vector->list monkeys)) >)) + (* (first is) (second is))) + +(define (part1) + (for ([_ (in-range 20)]) + (round! monkeys1 #{floor (/ % 3)})) + (monkey-business monkeys1)) + +(define (part2) + (for ([_ (in-range 10000)]) + (round! monkeys2 #{modulo % modulus})) + (monkey-business monkeys2)) + +(show-solution* part1 part2) \ No newline at end of file