day 11: DRY
This commit is contained in:
parent
e8d262b553
commit
88832b8b92
47
11.rkt
47
11.rkt
|
@ -7,43 +7,7 @@
|
|||
|
||||
;; solution for day 11
|
||||
|
||||
(define (part1 input)
|
||||
(dbg input)
|
||||
|
||||
(define (m-op n) (second (hash-ref input n)))
|
||||
(define (m-test n) (third (hash-ref input n)))
|
||||
(define (m-if-t n) (fourth (hash-ref input n)))
|
||||
(define (m-if-f n) (fifth (hash-ref input n)))
|
||||
|
||||
(define (m-op* n worry)
|
||||
(define op (m-op n))
|
||||
((match (first op) ['+ +] ['* *])
|
||||
(match (second op) ['old worry] [x x])
|
||||
(match (third op) ['old worry] [x x])))
|
||||
|
||||
(define inspect-count (make-hash))
|
||||
(for ([k (in-hash-keys input)])
|
||||
(hash-set! inspect-count k 0))
|
||||
|
||||
(for*/fold ([monkeys (for/hash ([(k v) (in-hash input)]) (values k (first v)))])
|
||||
([rnd (in-range 20)] [mky (in-hash-keys input)])
|
||||
(define items (hash-ref monkeys mky))
|
||||
|
||||
(for/fold ([monkeys (hash-set monkeys mky '())])
|
||||
([item (in-list items)])
|
||||
(hash-update! inspect-count mky add1)
|
||||
(define new-worry (~> item (m-op* mky _) (/ _ 3) (floor)))
|
||||
(define next-mky
|
||||
(if (zero? (remainder new-worry (m-test mky))) (m-if-t mky) (m-if-f mky)))
|
||||
(hash-update monkeys next-mky #{append % (list new-worry)})))
|
||||
|
||||
(match-define (list* top0 top1 rst) (sort (hash-values inspect-count) >))
|
||||
(* top0 top1))
|
||||
|
||||
|
||||
(define (part2 input)
|
||||
(dbg input)
|
||||
|
||||
(define ((part div3?) input)
|
||||
(define (m-op n) (second (hash-ref input n)))
|
||||
(define (m-test n) (third (hash-ref input n)))
|
||||
(define (m-if-t n) (fourth (hash-ref input n)))
|
||||
|
@ -63,13 +27,14 @@
|
|||
(apply lcm (for/list ([mky (in-hash-keys input)]) (m-test mky))))
|
||||
|
||||
(for*/fold ([monkeys (for/hash ([(k v) (in-hash input)]) (values k (first v)))])
|
||||
([rnd (in-range 10000)] [mky (in-hash-keys input)])
|
||||
([rnd (in-range (if div3? 20 10000))] [mky (in-hash-keys input)])
|
||||
(define items (hash-ref monkeys mky))
|
||||
|
||||
(for/fold ([monkeys (hash-set monkeys mky '())])
|
||||
([item (in-list items)])
|
||||
(hash-update! inspect-count mky add1)
|
||||
(define new-worry (~> item (m-op* mky _) (remainder _ all-monkeys-modulo)))
|
||||
(define new-worry (~> item (m-op* mky _) (#{if div3? (quotient % 3) %})
|
||||
(remainder _ all-monkeys-modulo)))
|
||||
(define next-mky
|
||||
(if (zero? (remainder new-worry (m-test mky))) (m-if-t mky) (m-if-f mky)))
|
||||
(hash-update monkeys next-mky #{append % (list new-worry)})))
|
||||
|
@ -119,6 +84,6 @@
|
|||
|
||||
(module+ main
|
||||
(define input (parse "inputs/11"))
|
||||
(answer 11 1 (time (part1 input)))
|
||||
(answer 11 2 (time (part2 input)))
|
||||
(answer 11 1 (time ((part #t) input)))
|
||||
(answer 11 2 (time ((part #f) input)))
|
||||
(displayln "meow"))
|
||||
|
|
Loading…
Reference in New Issue