Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

28
2022/day11/day11.bqn Normal file
View file

@ -0,0 +1,28 @@
lf,Split,ToNats•Import"../bqn-libs/strings.bqn"
ms>lfSplit¨(2lf)Split ¯1•FChars•args
itemsToNats¨1¨<˘ms
tests{𝕊d,t,f: {(0=d|𝕩)ft}}˘¨ToNats¨3˘ms
op{𝕩"* old" ? ט; 𝕩"+ old" ? +˜; '*'𝕩 ? (ToNats𝕩)×; '+'𝕩 ? (ToNats𝕩)+}¨23¨2˘ms
mod×´¨ToNats¨3¨<˘ms
MonkeyBusiness{part𝕊items:
inspected(items)0
Inspect{part𝕊i:
opiop
testitests
Throw{
it(part-1)÷3 Op 𝕩,mod|Op 𝕩
itemsit((Test it))items
}
Throw¨ iitems
inspected(iitems)+(i)inspected
items˙(i)items
}
Round{partInspect¨𝕩}
Round((part-1)2010000) items
# •Show inspected
×´2inspected
}
•Show 1 MonkeyBusiness items
•Show 2 MonkeyBusiness items

75
2022/day11/day11.rkt Normal file
View file

@ -0,0 +1,75 @@
#lang racket
(require data/queue
threading)
(struct monkey
(items op test divisor (inspections #:mutable))
#:transparent)
(define (read-input filename worry-reduction)
(for/vector ([mstr (string-split (file->string filename) "\n\n")])
(define infos (string-split mstr "\n"))
(define items (make-queue))
(for ([item (map string->number (string-split (substring (cadr infos) 18) ", "))])
(enqueue! items item))
(define op
(compose1
(lambda (n) (quotient n worry-reduction))
(match (string-split (substring (caddr infos) 23))
[(list "+" "old") (lambda (x) (+ x x))]
[(list "*" "old") (lambda (x) (* x x))]
[(list "+" n) (lambda (x) (+ x (string->number n)))]
[(list "*" n) (lambda (x) (* x (string->number n)))])))
(match-define (list d a b) (map (compose1 string->number last string-split) (cdddr infos)))
(define (test n)
(if (zero? (remainder n d)) a b))
(monkey items op test d 0)))
(define (throw! item to)
(enqueue! (monkey-items to) item))
(define (process-item! ms m (mod #f))
(set-monkey-inspections! m (+ 1 (monkey-inspections m)))
(define item ((monkey-op m) (dequeue! (monkey-items m))))
(when mod
(set! item (remainder item mod)))
(throw! item (vector-ref ms ((monkey-test m) item))))
(define (run-round! ms (mod #f))
(let loop ([i 0])
(cond
[(= i (vector-length ms)) ms]
[(queue-empty? (monkey-items (vector-ref ms i))) (loop (+ 1 i))]
[#t (begin (process-item! ms (vector-ref ms i) mod)
(loop i))])))
(define (run-rounds! ms n (mod #f) (display? #f))
(for ([i (in-range n)])
(run-round! ms mod)
(when display? (display-items ms))))
(define (display-items ms)
(for ([m ms]
[i (in-naturals)])
(printf "Monkey ~a: ~a~n"
i
(string-join (map number->string (queue->list (monkey-items m))) ", "))))
(define (inspection-levels ms)
(vector->list (vector-map monkey-inspections ms)))
(define (monkey-business inspections)
(~> inspections
(sort >=)
(take 2)
(apply * _)))
(module+ main
(let* ([ms (read-input "input" 3)])
(run-rounds! ms 20)
(displayln (monkey-business (inspection-levels ms))))
(let* ([ms (read-input "input" 1)]
[mod (apply * (vector->list (vector-map monkey-divisor ms)))])
(run-rounds! ms 10000 mod)
(displayln (monkey-business (inspection-levels ms)))))

55
2022/day11/input Normal file
View file

@ -0,0 +1,55 @@
Monkey 0:
Starting items: 92, 73, 86, 83, 65, 51, 55, 93
Operation: new = old * 5
Test: divisible by 11
If true: throw to monkey 3
If false: throw to monkey 4
Monkey 1:
Starting items: 99, 67, 62, 61, 59, 98
Operation: new = old * old
Test: divisible by 2
If true: throw to monkey 6
If false: throw to monkey 7
Monkey 2:
Starting items: 81, 89, 56, 61, 99
Operation: new = old * 7
Test: divisible by 5
If true: throw to monkey 1
If false: throw to monkey 5
Monkey 3:
Starting items: 97, 74, 68
Operation: new = old + 1
Test: divisible by 17
If true: throw to monkey 2
If false: throw to monkey 5
Monkey 4:
Starting items: 78, 73
Operation: new = old + 3
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 5:
Starting items: 50
Operation: new = old + 5
Test: divisible by 7
If true: throw to monkey 1
If false: throw to monkey 6
Monkey 6:
Starting items: 95, 88, 53, 75
Operation: new = old + 8
Test: divisible by 3
If true: throw to monkey 0
If false: throw to monkey 7
Monkey 7:
Starting items: 50, 77, 98, 85, 94, 56, 89
Operation: new = old + 2
Test: divisible by 13
If true: throw to monkey 4
If false: throw to monkey 0

27
2022/day11/test Normal file
View file

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