Matthew Butterick 7 years ago
parent 30a2928e99
commit bbbb575569

@ -0,0 +1,28 @@
#lang reader "../aoc-lang.rkt"
(provide (rename-out [#%mb #%module-begin]))
(define-macro (#%mb (STARS) (BANK ...))
#`(#%module-begin
(count-redists (list->vector '(BANK ...)) 'STARS)))
(define (redist starting-vec)
(define vec (vector-copy starting-vec))
(define max-blocks (vector-argmax values vec))
(define start-at (vector-member max-blocks vec))
(vector-set! vec start-at 0)
(for ([block (in-range max-blocks)]
[idx (in-cycle (shift-left-cycle (range (vector-length vec)) (add1 start-at)))])
(vector-set! vec idx (+ (vector-ref vec idx) 1)))
vec)
(define (count-redists bankvec stars)
(let/ec exit
(for/fold ([bankvecs-seen (list bankvec)])
([i (in-naturals)])
(cond
[(member (car bankvecs-seen) (cdr bankvecs-seen))
=> (λ (tail) (exit (if (eq? stars ')
i
(- (length bankvecs-seen) (length tail)))))])
(cons (redist (car bankvecs-seen)) bankvecs-seen))))

@ -0,0 +1,2 @@
#lang reader "main.rkt" ; 14029
10 3 15 10 5 15 5 15 9 2 5 8 5 2 3 6

@ -0,0 +1,2 @@
#lang reader "main.rkt" ★★ ; 2765
10 3 15 10 5 15 5 15 9 2 5 8 5 2 3 6

@ -0,0 +1,2 @@
#lang reader "main.rkt"
0 2 7 0

@ -0,0 +1,2 @@
#lang reader "main.rkt" ★★
0 2 7 0