đź§µ Day 21 Solutions Thread: post your solution here
https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day21.clj
@nbardiuk Like your approach for switching between two players. I always forget about it
refactored my solution with this approach, but basically I got almost the same https://github.com/zelark/AoC-2021/blob/main/src/zelark/aoc_2021/day_21.clj
that was a fun one. was stuck for a bit because I forgot you had to roll the dice three times, and was confused why I had so many fewer universes than in the example… same approach as everyone else by the looks of it. 2ms/7ms https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/2021/21.clj
I think my approach for part 2 is a bit different, i basically count the universes where each player wins or loses after X turns then the number of wins for each turn is the number of universes where the other player does not win in the turn X-1 * the universes where they win in turn X
turns out slower than @c.oakley108, but maybe it's my code?
Lol
Yeah pretty much every thread on Reddit has this discussion going on
i don’t loop, just iterate through whole state space, to track all combos for the visualization I’ll never get around to. 1s part-2.
I do like my fancy mod-1 function to handle base 1 mod, which I went back and applied to some prev problems
(defn mod-1
"Returns the 1-based modulus `base` of `n`"
[n base]
(inc (mod (dec n) base)))nice problem to illustrate the difference between “single recursion” and “multiple recursion” though. I actually like that clojure’s recur makes the distinction obvious at a glance (though of course automatic tail call optimisation would be cool…)
@tws let me stole your lovely mod-1 🙂
that’s what I’m here for. did you see this one? would clean up a few people’s mixed threading woes.
(defn x-nth
"Returns the `n`th element of sequence `xs`. Works like `clojure.core/nth` but
reverses the order of the arguments so we can use in thread-last pipelines."
[n xs]
(nth xs n))and can you share the link pls, I want to see your solution in clojure. Today I saw one in Python, but it is all about mutating state
kinda gross right now, cleaning up
yeah, I did, but I prefer (->> coll (drop n …) first) ^_^
madman
@zelark here’s my immutable solution ~1.2s, no recursion or memoization. writeup: https://github.com/tschady/advent-of-code/blob/main/doc/2021.adoc#aoc2021d21 code: https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d21.clj
so here are the total number of different states at each time tick:
(1 7 49 343 2401 5733 11232 8832 7268 5530 3640 2548 1519 775 375 150 30 3 0)
@tws thanks, I’ll check it tomorrow, need some sleep now I also tried one, works fast ~300msecs, but looks a bit ugly
(defn qgame [cache [current another :as state]]
(cond
(>= (:score another) 21) [cache [0 1]]
(cache state) [cache (cache state)]
:else
(let [[cache' score']
(reduce (fn [[cache [a b]] [steps freq]]
(let [new-position (move (current :position) steps)
current' (-> (assoc current :position new-position)
(update :score + new-position))
[cache' [a' b']] (qgame cache [another current'])]
[cache' [(+ a (* b' freq)) (+ b (* a' freq))]]))
[cache [0 0]]
[[3 1] [4 3] [5 6] [6 7] [7 6] [8 3] [9 1]])]
[(assoc cache' state score') score'])))Slow and verbose: https://samadams.dev/2021/12/21/advent-of-code-day-21.html This one was very satisfying — but I also fumbled around for a while forgetting that 3 dice were rolled per turn.
I think mine could be adapted to be used with iterate ! Because I generate the possible states at each turn but group them by winning and not winning and just count
I'll try that tomorrow :)
https://github.com/kconner/advent-of-code/blob/master/2021/21a.clj, https://github.com/kconner/advent-of-code/blob/master/2021/21b.clj, 11ms and 51s.
my first attempt at part 2 allocated 40GB before I killed it… then I came back to try DFS, which worked better. using pmap to fork over the first few depths was useful too.
https://gitlab.com/maximoburrito/advent2021/-/blob/main/src/day21/main.clj sloppy code as usual, but I think the approach is solid