Fork me on GitHub

🧵 Day 21 Solutions Thread: post your solution here


@U076FM90B Like your approach for switching between two players. I always forget about it

👍 1

refactored my solution with this approach, but basically I got almost the same

Callum Oakley13:12:45

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

Antonio Bibiano15:12:01

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

Antonio Bibiano15:12:25

turns out slower than @U01HL2S0X71, but maybe it's my code?

Antonio Bibiano17:12:35

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)))

👌 3
Callum Oakley18:12:12

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…)


@U1Z392WMQ 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) ^_^


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)


@U1Z392WMQ 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]]
    (>= (:score another) 21) [cache [0 1]]
    (cache state)            [cache (cache state)]
    (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'])))

Sam Adams21:12:39

Slow and verbose: This one was very satisfying — but I also fumbled around for a while forgetting that 3 dice were rolled per turn.

Antonio Bibiano21:12:42

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

Antonio Bibiano21:12:57

I'll try that tomorrow :)


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.