From 7fd928a370371f6aaa0b34f7f7ce63ea8c35baf2 Mon Sep 17 00:00:00 2001 From: Hunky Jimpjorps Date: Wed, 21 Dec 2022 16:16:23 -0500 Subject: day 21 simplification --- 2022/day-21/day-21.rkt | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to '2022/day-21') diff --git a/2022/day-21/day-21.rkt b/2022/day-21/day-21.rkt index 9f08f97..fccd6ad 100644 --- a/2022/day-21/day-21.rkt +++ b/2022/day-21/day-21.rkt @@ -33,18 +33,11 @@ (evaluate-monkey "root") ;; part 2 +;; since humn only ever appears once, and it's never the divisor in a division operation, +;; the difference of the branches is linearly proportional to humn +;; therefore, if we find two points we can calculate the root directly (match-define (op _ branch-1 branch-2) (hash-ref monkey-table "root")) - -;; the branch that doesn't depend on humn (define known-side (evaluate-monkey branch-2)) - -(for/fold ([lower-bound 0] [upper-bound 1e16] #:result (inexact->exact lower-bound)) - ([_ (in-naturals)]) - #:break (= lower-bound upper-bound) - (define midpoint (quotient (+ lower-bound upper-bound) 2)) - (define candidate (evaluate-monkey branch-1 midpoint)) - (println (~a midpoint " -> " candidate " -> " (- candidate known-side))) - (cond - [(= candidate known-side) (values midpoint midpoint)] - [(> candidate known-side) (values midpoint upper-bound)] - [(< candidate known-side) (values lower-bound midpoint)])) +(define humn-zero (- known-side (evaluate-monkey branch-1 0))) +(define humn-one (- known-side (evaluate-monkey branch-1 1))) +(- (/ humn-zero (- humn-one humn-zero))) -- cgit v1.2.3