From a37c0763436301cf5d148fad7639ea49faa76729 Mon Sep 17 00:00:00 2001 From: haskal Date: Fri, 25 Dec 2020 00:26:56 -0500 Subject: [PATCH] day 25: make it nicer --- 25.rkt | 43 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/25.rkt b/25.rkt index abed557..30e8692 100644 --- a/25.rkt +++ b/25.rkt @@ -1,51 +1,28 @@ #lang curly-fn racket -(require "scripts/aoc.rkt") +(require "scripts/aoc.rkt" math/number-theory) ;; solution for day 25 (define *mod* 20201227) - -(define (loop subject times) - (for/fold ([val 1]) ([_ (in-range (+ 0 times))]) - (remainder (* val subject) *mod*))) +(define *subject* 7) (define (part1 input) (match-define (list you door) input) - (define you-loops #f) - (define door-loops #f) - (for/fold ([val 1]) ([loops (in-range 0 +inf.0)] #:break (and you-loops door-loops)) - (when (= val you) - (set! you-loops loops)) - (when (= val door) - (set! door-loops loops)) - (remainder (* val 7) *mod*)) - ; (displayln you-loops) - ; (displayln door-loops) - (displayln you-loops) - (displayln (loop 7 you-loops)) - (displayln you) - (displayln door-loops) - (displayln (loop 7 door-loops)) - (displayln door) - ; (displayln (loop door-loops you-loops)) - ; (displayln (loop you-loops door-loops)) - ; (displayln (loop 17807724 8)) - ; (displayln (loop 5764801 11)) - (loop you door-loops)) -; (part1 (list 5764801 17807724)) -; (error) + (with-modulus *mod* + (for/fold ([val 1] [you-loops #f] [door-loops #f] #:result (modexpt you door-loops)) + ([loops (in-range 0 +inf.0)] #:break (and you-loops door-loops)) + (define new-val (mod* val *subject*)) + (cond + [(= val you) (values new-val loops door-loops)] + [(= val door) (values new-val you-loops loops)] + [else (values new-val you-loops door-loops)])))) ;; parse input file (define (parse fname) (define input (file->lines fname)) (map string->number input)) -(module+ test - (require rackunit) - ;; tests here - (displayln "no tests :(")) - (module+ main (define input (parse "inputs/25")) (answer 25 1 (time (part1 input)))