2016 d16 p2

master
Matthew Butterick 5 years ago
parent 7a0a2ca36f
commit 8c1bf08ebe

@ -10,14 +10,14 @@
(define-macro (mb SIZE INIT) (define-macro (mb SIZE INIT)
#'(#%module-begin #'(#%module-begin
(display (list->string (checksum (fill-disk (string->list INIT) (string->number SIZE))))))) (time (display (list->string (checksum (fill-disk (string->list INIT) (string->number SIZE))))))))
(define (dragonize cs) (define (dragonize cs)
(append cs '(#\0) (append cs '(#\0)
(for/list ([c (in-list (reverse cs))]) (for/list ([c (in-list (reverse cs))])
(if (eqv? c #\1) (if (eqv? c #\1)
#\0 #\0
#\1)))) #\1))))
(define (fill-disk init size) (define (fill-disk init size)
(let loop ([cs init]) (let loop ([cs init])
@ -25,14 +25,15 @@
(take cs size) (take cs size)
(loop (dragonize cs))))) (loop (dragonize cs)))))
(require sugar/list)
(define (checksum cs) (define (checksum cs)
(let loop ([cs cs]) (define cvec (list->vector cs))
(if (odd? (length cs)) (let loop ([cvec cvec])
cs (match (vector-length cvec)
(loop [(? odd?) (vector->list cvec)]
(for/list ([match (in-list (slice-at cs 2))]) [len
(if (member match '((#\0 #\0) (#\1 #\1))) (define newvec (make-vector (/ len 2) #\0))
#\1 (for ([idx (in-range 0 (vector-length cvec) 2)]
#\0)))))) #:when (char=? (vector-ref cvec idx) (vector-ref cvec (add1 idx))))
(vector-set! newvec (/ idx 2) #\1))
(loop newvec)])))