First puzzle: https://github.com/pesterhazy/advent2019
My entry: https://github.com/mpcjanssen/aoc2019
Although (iterate)
seems perfect for this puzzle.
I've tried to induce recursion formula to get the sum of the sequence, but it was slower than I expected because it requires factorizations. π https://github.com/namenu/advent-of-code/blob/master/src/year2019/day01.clj
Started with a transducer + recursive function solution for the second task. But after seeing the iterator based one I switched out the recursive function. https://github.com/snorremd/adventofcode2019/blob/master/src/day1.clj
oh man, I was able to complete both parts in about 20 minutes today! Might not sound like much but this time last year, that would have been a tough ask. Thank you Clojure slack for all the help! That was so satisfying inputting correct answers on the first try. This is fun!
now we shall see how quickly the difficulty ramps up
(def FILE "../data/1.txt")
(def data (~>> FILE file->lines (map string->number)))
;; βββββββββββββββββββββββββββββββββββββββββββββββ
;; Solve
(fn (step n) (~> n (quotient _ 3) (- 2)))
(fn (score n) (apply + (unfold negative? identity step (step n))))
(def A (~>> data (map step) (apply +)))
(def B (~>> data (map score) (apply +)))
@slack1899 what language is that?
TIL you canβt just shove #(/ % 3)
into a ->
threading macro, even though you can normally shove functions that take a single arg. No idea whyβ¦
at this point I realize #(/ % 3)
was unnecessary for a ->
, but still π€·
Because thatβs expanded at read time to (fn [temp] ...) so it gets threaded as (fn thread-value [temp] ...)
ahhh, that makes a lot of sense
thank you!
Probably catches everyone at some point
@dpsutton this is Racket but I couldn't help but snoop around the other Lisp channels I'm a part of
I only use Racket for practicing algorithms and puzzles and toys
Mine π https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2019/day1.clj
Ah iterate
whatβs annoying is that since the mass->fuel calc is lossy, you get different results if you calculate on the fuel requirements for the sum of all mass, as opposed to calculating the fuel requirements for each module of mass and summing the fuel results
I always realize how much I stink at Clojure after reading other peopleβs code π
is there any real advantage to using transduce
over reduce
?
I didnβt know you could use Clojure in a Jupyter notebook. Nice
speed & memory
fewer allocations for intermediate structures
but unless youβre wanting to learn to use transducers, it doesnβt matter for this problem
My transducer version took 3 times as long to run as my reduce version. Maybe with more data or fns in the transducer compose it would swing the other way?