aboutsummaryrefslogtreecommitdiff
path: root/2022/day-21
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-21 16:16:23 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-21 16:16:23 -0500
commit7fd928a370371f6aaa0b34f7f7ce63ea8c35baf2 (patch)
tree9c35595c5a0c43e8aab1d36f5b1e2b4f4ae6743b /2022/day-21
parent3edaf521fe2102270563cf8c00bfa36b5486a7e6 (diff)
downloadgleam_aoc-7fd928a370371f6aaa0b34f7f7ce63ea8c35baf2.tar.gz
gleam_aoc-7fd928a370371f6aaa0b34f7f7ce63ea8c35baf2.zip
day 21 simplification
Diffstat (limited to '2022/day-21')
-rw-r--r--2022/day-21/day-21.rkt19
1 files changed, 6 insertions, 13 deletions
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)))