From fa1327e70cacd2250769ac9d444de3dace920fca Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 12 Dec 2019 14:06:58 -0800 Subject: [PATCH] d12 p2 --- 2019/12.rkt | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/2019/12.rkt b/2019/12.rkt index 0abf18b..0409eff 100644 --- a/2019/12.rkt +++ b/2019/12.rkt @@ -8,7 +8,7 @@ (define (str->moons str) (for/list ([trip (in-slice 3 (parse-input str))]) - ($moon trip (list 0 0 0)))) + ($moon trip (list 0 0 0)))) (define (update-pair-velocity! m0 m1) (define-values (new-m0-vel new-m1-vel) @@ -34,20 +34,20 @@ (unless (= 1 (length moons)) (match-define (cons m0 ms) moons) (for ([m (in-list ms)]) - (update-pair-velocity! m0 m)) + (update-pair-velocity! m0 m)) (loop ms)))) (define (step! moons [count 1]) (for ([i (in-range count)]) - (update-gravity! moons) - (for-each update-position! moons)) + (update-gravity! moons) + (for-each update-position! moons)) moons) (define (total-energy moons) (for/sum ([moon (in-list moons)]) - (apply * (for/list ([field-proc (list $moon-pos $moon-vel)]) - (for/sum ([val (in-list (field-proc moon))]) - (abs val)))))) + (apply * (for/list ([field-proc (list $moon-pos $moon-vel)]) + (for/sum ([val (in-list (field-proc moon))]) + (abs val)))))) (check-eq? (total-energy (step! (str->moons " @@ -69,36 +69,35 @@ 9876) ;; 2 -(define (shift-origin! moons pos) - (for ([moon (in-list moons)]) - (set-$moon-pos! moon (map - ($moon-pos moon) pos)))) - (define (period str) - (for*/list ([which-moon (list first second third fourth)] - [which-dim (list first second third)]) - (define moons (str->moons str)) - (shift-origin! moons ($moon-pos (which-moon moons))) - (let loop ([count 1]) - (step! moons) - (if (and (= 0 (which-dim ($moon-pos (which-moon moons)))) - (= 0 (which-dim ($moon-vel (which-moon moons))))) - count - (loop (add1 count)))))) + (apply lcm + (for*/list ([dim (list first second third)]) + (define moons (str->moons str)) + (define (get-dims) (for*/list ([moon (in-list moons)] + [field (in-list (list $moon-pos $moon-vel))]) + (dim (field moon)))) + (define dims0 (get-dims)) + (let loop ([count 0]) + (step! moons) + (if (andmap = dims0 (get-dims)) + (add1 count) + (loop (add1 count))))))) (check-eq? - (apply lcm (period " + (period " -")) +") 2772) (check-eq? - (apply lcm - (period " + (period " -")) - 4686774924) +") + 4686774924) + +(check-eq? (period (file->string "12.rktd")) 307043147758488)