Matthew Butterick 8 years ago
parent f86149e4df
commit 9013625d23

@ -9,7 +9,7 @@
(define-macro (mb NUM-STR) (define-macro (mb NUM-STR)
#'(#%module-begin #'(#%module-begin
(displayln (solve (string->number NUM-STR) #f)) #;(displayln (solve (string->number NUM-STR) #f))
(displayln (solve (string->number NUM-STR) #t)))) (displayln (solve (string->number NUM-STR) #t))))
(define (idx-after vec x) (define (idx-after vec x)
@ -17,7 +17,7 @@
(for/first ([idx (in-range (modulo (add1 x) (vector-length vec)) (vector-length vec))] (for/first ([idx (in-range (modulo (add1 x) (vector-length vec)) (vector-length vec))]
#:when (vector-ref vec idx)) #:when (vector-ref vec idx))
idx) idx)
(idx-after vec 0))) (idx-after vec -1)))
(define (solve num [circle? #f]) (define (solve num [circle? #f])
(define elves (make-vector num #t)) (define elves (make-vector num #t))
@ -26,7 +26,7 @@
[(= elves-left 2) (add1 taker)] [(= elves-left 2) (add1 taker)]
[else [else
(define giver (for/fold ([elf taker]) (define giver (for/fold ([elf taker])
([i (in-range (if circle? (floor (/ num 2)) 1))]) ([i (in-range (if circle? (floor (/ elves-left 2)) 1))])
(idx-after elves elf))) (idx-after elves elf)))
(vector-set! elves giver #f) (vector-set! elves giver #f)
(loop (idx-after elves taker) (sub1 elves-left))]))) (loop (idx-after elves taker) (sub1 elves-left))])))