@popa.bogdanp has joined the channel
@badkins has joined the channel
I have a question about Day 1 Part 2. I’ll put it in a thread to avoid spoilers.
Did anyone generalize to N, or did you just modify your solution for 2 to work specifically for 3?
I’ve added a <https://github.com/lojic/LearningRacket/tree/master/advent-of-code–2020|Advent of Code 2020 directory> to my <https://github.com/lojic/LearningRacket|Learning Racket repo>
@johnmwise has joined the channel
I adapted mine. Now that I squint generalizing it shouldn’t be too hard.
I had to think a bit harder than I should have to create find-n
- seems like a perfect problem for lisps.
I’m not super pleased with the end result, so if anyone comes up with a more elegant find-n
, let me know: https://github.com/lojic/LearningRacket/blob/09b032f0ae7edcbb493954eec4e0bc53a417e24f/advent-of-code-2020/day1b.rkt#L28-L48
Does macro-based generalization count? :stuck_out_tongue: https://github.com/Bogdanp/aoc2020/blob/8a43b3e6fc7c7bad6c4f050f4b05c542e41b9977/day01.rkt#L26-L38
I’m scared, and intrigued, to see what you have cooked up.
Looking at Racket’s cartesian-product
: (define (cartesian-product . ls)
(define (cp-2 as bs)
(for*/list ([i (in-list as)] [j (in-list bs)]) (cons i j)))
(foldr cp-2 (list (list)) ls))
I’m pretty sure there is something similarly elegant that combines elements in a way needed for this task.
I’m fine with not short circuiting - probably better to find all solutions anyway.
Ok, I feel more comfortable with the following solution to a slightly different problem: (define (ascending-permutations lst n)
(reverse
(let loop ([ lst lst ][ n n ][ stack '() ][ result '() ])
(if (= n 0)
(cons (reverse stack) result)
(if (null? lst)
result
(let ([ ans (loop (cdr lst) (sub1 n) (cons (car lst) stack) result) ])
(loop (cdr lst) n stack ans)))))))
Produces all the “ascending permutations” - I’m really not sure what to call them.
Maybe making this into a generator would do the job.
Hmm… no need for the ans
variable: (define (ascending-permutations lst n)
(reverse
(let loop ([ lst lst ][ n n ][ stack '() ][ result '() ])
(if (= n 0)
(cons (reverse stack) result)
(if (null? lst)
result
(loop (cdr lst)
n
stack
(loop (cdr lst) (sub1 n) (cons (car lst) stack) result)))))))
@eeide has joined the channel