1
0
Fork 0
This commit is contained in:
Jonathan Chan 2022-12-11 01:47:29 -05:00
parent 5d0921f1fd
commit bbed337805
3 changed files with 111 additions and 0 deletions

55
input/11.txt Normal file
View File

@ -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

View File

@ -212,6 +212,10 @@
;; % : number -> number -> number ;; % : number -> number -> number
(define % modulo) (define % modulo)
;; divisible? : number -> number -> number
(define (divisible? n m)
(= (% n m) 0))
;; number->digits-reverse : number -> (listof number) ;; number->digits-reverse : number -> (listof number)
;; Return the digits of the given number in reverse order (i.e. RTL) ;; Return the digits of the given number in reverse order (i.e. RTL)
(define (number->digits-reverse n) (define (number->digits-reverse n)

52
src/11.rkt Normal file
View File

@ -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)