day 25: make it nicer
This commit is contained in:
parent
f782867296
commit
a37c076343
43
25.rkt
43
25.rkt
|
@ -1,51 +1,28 @@
|
||||||
#lang curly-fn racket
|
#lang curly-fn racket
|
||||||
|
|
||||||
(require "scripts/aoc.rkt")
|
(require "scripts/aoc.rkt" math/number-theory)
|
||||||
|
|
||||||
;; solution for day 25
|
;; solution for day 25
|
||||||
|
|
||||||
(define *mod* 20201227)
|
(define *mod* 20201227)
|
||||||
|
(define *subject* 7)
|
||||||
(define (loop subject times)
|
|
||||||
(for/fold ([val 1]) ([_ (in-range (+ 0 times))])
|
|
||||||
(remainder (* val subject) *mod*)))
|
|
||||||
|
|
||||||
(define (part1 input)
|
(define (part1 input)
|
||||||
(match-define (list you door) input)
|
(match-define (list you door) input)
|
||||||
(define you-loops #f)
|
(with-modulus *mod*
|
||||||
(define door-loops #f)
|
(for/fold ([val 1] [you-loops #f] [door-loops #f] #:result (modexpt you door-loops))
|
||||||
(for/fold ([val 1]) ([loops (in-range 0 +inf.0)] #:break (and you-loops door-loops))
|
([loops (in-range 0 +inf.0)] #:break (and you-loops door-loops))
|
||||||
(when (= val you)
|
(define new-val (mod* val *subject*))
|
||||||
(set! you-loops loops))
|
(cond
|
||||||
(when (= val door)
|
[(= val you) (values new-val loops door-loops)]
|
||||||
(set! door-loops loops))
|
[(= val door) (values new-val you-loops loops)]
|
||||||
(remainder (* val 7) *mod*))
|
[else (values new-val you-loops door-loops)]))))
|
||||||
; (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)
|
|
||||||
|
|
||||||
;; parse input file
|
;; parse input file
|
||||||
(define (parse fname)
|
(define (parse fname)
|
||||||
(define input (file->lines fname))
|
(define input (file->lines fname))
|
||||||
(map string->number input))
|
(map string->number input))
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
;; tests here
|
|
||||||
(displayln "no tests :("))
|
|
||||||
|
|
||||||
(module+ main
|
(module+ main
|
||||||
(define input (parse "inputs/25"))
|
(define input (parse "inputs/25"))
|
||||||
(answer 25 1 (time (part1 input)))
|
(answer 25 1 (time (part1 input)))
|
||||||
|
|
Loading…
Reference in New Issue