diff options
Diffstat (limited to '2022')
-rw-r--r-- | 2022/day-21/day-21.rkt | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/2022/day-21/day-21.rkt b/2022/day-21/day-21.rkt index 752dfb8..9f08f97 100644 --- a/2022/day-21/day-21.rkt +++ b/2022/day-21/day-21.rkt @@ -22,9 +22,7 @@ ;; part 1 (define (evaluate-monkey m-name [guess #f]) (match-define (op f name1 name2) - (if (and guess (equal? m-name "humn")) - (op 'constant guess #f) - (hash-ref monkey-table m-name))) + (if (and guess (equal? m-name "humn")) (op 'constant guess #f) (hash-ref monkey-table m-name))) (match f ['constant name1] ['+ (+ (evaluate-monkey name1 guess) (evaluate-monkey name2 guess))] @@ -38,11 +36,15 @@ (match-define (op _ branch-1 branch-2) (hash-ref monkey-table "root")) ;; the branch that doesn't depend on humn -(define result-2 (evaluate-monkey branch-2)) - -;; I plugged in numbers for guess to find a suitable starting range and settled on the first one -;; that I found that got me within a million -(for/first ([guess (in-naturals 3059361690000)] - #:do [(define result-1 (evaluate-monkey branch-1 guess))] - #:when (= result-1 result-2)) - guess) +(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)])) |