badkins
2020-12-2 22:07:52

Oh, the leaderboard is in order of how soon you post your answer - I guess I’m doomed to the bottom since I’ll probably be doing these late in the day :)


badkins
2020-12-2 22:08:18

Doesn’t change the level of fun though.


notjack
2020-12-2 22:11:39

honestly if you consistently do them every day I’m sure you’ll be ahead of most people regardless of what time during the day you do them


badkins
2020-12-2 22:50:24

@winny your Day 1 solution is pretty amazing IMO ! https://github.com/winny-/aoc/blob/master/2020/01/day01.rkt


badkins
2020-12-2 22:51:38

Might miss some solutions in a different input file, but the concision is incredible :)


winny
2020-12-2 22:52:25

Thanks. :-) Part two takes awhile due to duplicate work, but i gave myself only 15 minutes to write it


badkins
2020-12-2 22:52:33

Just out of curiosity, have you spent much time w/ Haskell or ML’s ?


winny
2020-12-2 22:53:15

Yes, some… sml and scala in school. Ocaml and haskell for fun/hacking on code


winny
2020-12-2 22:54:12

But really racket is my first exposure to fp, besides learning list processing stuff in python list comprehensions and .net LINQ


badkins
2020-12-2 22:55:05

I need to get more comfortable with folds, etc. I felt pretty good about the following: (define (ascending-permutations n lst) (cond [ (null? lst) '() ] [ (= n 1) (map (λ (e) (list e)) lst) ] [ else (append (foldr (λ (e result) (cons (cons (car lst) e) result)) '() (ascending-permutations (sub1 n) (cdr lst))) (ascending-permutations n (cdr lst))) ])) but had to think about it for a while, and that’s only part of a solution vs. your one line complete solution :)


badkins
2020-12-2 22:56:11

Coming from Assembler/C/C++ in the old days, it’s hard for me to not consider efficiency, but sometimes, a concise, readable solution is best, even if a bit slower.


badkins
2020-12-2 23:09:38

@winny I just noticed you could probably just apply * instead of that first fold: (apply * (for/first ([c (combinations (port->list) 2)] #:when (= 2020 (foldl + 0 c))) c))


badkins
2020-12-2 23:10:05

Pretty amazing that Racket can solve Day 1 Part 1 in one line like that - thanks for the inspiration!


winny
2020-12-2 23:23:37

Goof catch! I’m always shy with apply. We know the list length is very short, so it’s probably similar time cost yet less verbosity. If it was very long list, apply might use a good amount of memory.


winny
2020-12-2 23:27:24

For every concise solution I wrote, I’ve probably deleted twice as many very verbose and wordy solutions… Keep playing with list processing and fp, and it’ll become very comfortable and intuitive in no time! I learn new stuff about fp every time i exercise this style too, so it keeps me engaged.


badkins
2020-12-2 23:50:29

Folds can be handy, but it does seem like list comprehensions are more readable (eye of the beholder I suppose): cartesian [] = [] cartesian [x] = [[e] \| e <- x] cartesian (x:xs) = [ x' : rest \| x' <- x, rest <- cartesian xs ] vs. cartesian [] = [] cartesian [x] = map (\ e -> [e]) x cartesian (x:xs) = foldr (\ x' result -> (foldr (\ tc l -> (x':tc):l) result tailCross)) [] x where tailCross = cartesian xs