From 92ab44f92a734e37b8f6c2c9c4983c9890346911 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 12 Dec 2019 06:45:38 -0800 Subject: [PATCH] d12 p2 (stuck) --- 2019/12.rkt | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/2019/12.rkt b/2019/12.rkt index 4e74251..0abf18b 100644 --- a/2019/12.rkt +++ b/2019/12.rkt @@ -37,7 +37,7 @@ (update-pair-velocity! m0 m)) (loop ms)))) -(define (step moons count) +(define (step! moons [count 1]) (for ([i (in-range count)]) (update-gravity! moons) (for-each update-position! moons)) @@ -50,14 +50,14 @@ (abs val)))))) (check-eq? - (total-energy (step (str->moons " + (total-energy (step! (str->moons " ") 10)) 179) (check-eq? - (total-energy (step (str->moons " + (total-energy (step! (str->moons " ") 100)) @@ -65,5 +65,40 @@ ;; 1 (check-eq? - (total-energy (step (str->moons (file->string "12.rktd")) 1000)) - 9876) \ No newline at end of file + (total-energy (step! (str->moons (file->string "12.rktd")) 1000)) + 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)))))) + +(check-eq? + (apply lcm (period " + + +")) + 2772) + +(check-eq? + (apply lcm + (period " + + +")) + 4686774924) + + +