
@caente has left the channel

Yeah, that was a great tip! I worked pretty hard on trying to exploit the similarity between parts for <https://github.com/lojic/LearningRacket/blob/master/advent-of-code–2020/day12.rkt|Day 12>, but I didn’t quite get to where I wanted.

I’m kind of surprised how few people have solved todays so far…

racket really had the import solution
for today lol

thank you racket :blue_heart:

yup! :smile:

I removed my second message becuase i figured it was a little too spoilery

spoilers for day 12 solution in thread

I cannot fucking believe I didn’t realize solve-chinese
is in the standard distribution until after I implemented my solution

oh no

I learned about about CRT in my intro to discrete mathematics class earlier this semester, so i’m glad there was one problem where it was a little easier for me to get a high score. That being said, i was aware of solve-chinese
after having looked through math/number-theory
from other day’s problems. I would not want to implement it myself, it was a big enough pain in class.

it was pretty dang painful implementing myself

however now I’m playing around with something neat

I made a congruence-equation?
struct > (congruence-equation 13 5)
x = 3 (mod 5)

why’s that? you needed to do some other modification with them?

wait, nvm. I’m dumb.

you said it was for implementing it yourself

tweaked the printing: > (congruence 13 5)
#<congruence: x = 3 (mod 5)>

very nice

(it bugs me a little that you aren’t using ≡ though)

oh I just made them because I like the idea of having a solve-chinese
function that takes a Listof Congruence
instead of two lists

you know what? you’re right. one sec.

> (congruence 13 5)
#<congruence: x ≡ 3 (mod 5)>

thanks :slightly_smiling_face:

thanks for saving me from having to google the ≡ character to copy paste it :p

that does sound nicer than the two lists thing

I made a for/lists
loop to make my two lists, which isn’t the nicest, but it works. I will try your Listof Congruence
approach tomorrow, it sounds nicer.

I’m tweaking it slightly now

Made a solve-congruences
function that takes a Sequenceof Congruence

(define (solve-congruences congruences)
(define descending-congruence-list
(transduce congruences (sorting #:key congruence-remainder #:descending? #true) #:into into-list))
(define remainders (map congruence-remainder descending-congruence-list))
(define moduli (map congruence-modulus descending-congruence-list))
(solve-chinese remainders moduli))

Are there performance benefits to sorting the list?

only if solve-chinese
is implemented with a sieve, which now that I think about it, it probably isn’t

uh… I have no idea what algorithm this is (define (solve-chinese as ns)
(unless (andmap positive? ns)
(raise-argument-error 'solve-chinese "(Listof Positive-Integer)" 1 as ns))
; the ns should be coprime
(let* ([n (apply * ns)]
[cs (map (λ: ([ni : Integer]) (quotient n ni)) ns)]
[ds (map modular-inverse cs ns)]
[es (cast ds (make-predicate (Listof Integer)))])
(cast (modulo (apply + (map * as cs es)) n) natural?)))

I didn’t sort my list and it seems to work fine for me (<2ms for the whole day)

since there were only a handful of congruences in the input problem for today it probably didn’t matter, I just did it because the wiki article on the sieve algorithm said it was a good idea

I’m gonna remove it from this one though since there’s not a sieve in sight

(define (solve-congruences congruences)
(define congruence-list (sequence->list congruences))
(define remainders (map congruence-remainder congruence-list))
(define moduli (map congruence-modulus congruence-list))
(solve-chinese remainders moduli))

Definitely a cleaner solution than mine, thanks for sharing! I should be getting to bed for tonight though, good luck on tomorrow’s problems!