aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-21 15:10:19 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-21 15:10:19 -0500
commit3edaf521fe2102270563cf8c00bfa36b5486a7e6 (patch)
tree4eb298daac4e7f232c688eb53dbae0706c6f6db2 /2022
parent7d83fb6a52dfbb5b96b9d2de6b9e06dd78e60faa (diff)
downloadgleam_aoc-3edaf521fe2102270563cf8c00bfa36b5486a7e6.tar.gz
gleam_aoc-3edaf521fe2102270563cf8c00bfa36b5486a7e6.zip
day 21 revision
Diffstat (limited to '2022')
-rw-r--r--2022/day-21/day-21.rkt24
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)]))