adventofcode

Aleks 2021-12-21T04:55:21.245700Z

đź§µ Day 21 Solutions Thread: post your solution here

nbardiuk 2021-12-21T09:02:24.248700Z

https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day21.clj

👍 1
👍🏻 2
Aleks 2021-12-21T10:34:45.250100Z

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

👍 1
Aleks 2021-12-21T12:49:31.250500Z

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

Callum Oakley 2021-12-21T13:09:45.250800Z

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

👏🏻 1
âž• 1
Antonio Bibiano 2021-12-21T15:47:01.251200Z

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 Bibiano 2021-12-21T15:48:55.252400Z

Antonio Bibiano 2021-12-21T15:54:25.253100Z

turns out slower than @c.oakley108, but maybe it's my code?

Aleks 2021-12-21T17:33:45.253500Z

Lol

Antonio Bibiano 2021-12-21T17:44:35.254500Z

Yeah pretty much every thread on Reddit has this discussion going on

tschady 2021-12-21T18:24:10.254700Z

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
🙌🏻 1
Callum Oakley 2021-12-21T18:24:12.254900Z

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

Aleks 2021-12-21T18:48:15.255600Z

@tws let me stole your lovely mod-1 🙂

tschady 2021-12-21T18:49:58.255800Z

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

👍🏻 1
Aleks 2021-12-21T18:50:33.256Z

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

tschady 2021-12-21T18:51:22.256300Z

kinda gross right now, cleaning up

Aleks 2021-12-21T18:51:37.256500Z

yeah, I did, but I prefer (->> coll (drop n …) first) ^_^

tschady 2021-12-21T19:00:38.256900Z

madman

tschady 2021-12-21T20:56:09.257100Z

@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

🙌🏻 1
tschady 2021-12-21T21:00:48.258100Z

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)

Aleks 2021-12-21T21:01:02.258300Z

@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'])))

Sam Adams 2021-12-21T21:24:39.259Z

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.

Antonio Bibiano 2021-12-21T21:45:42.261600Z

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 Bibiano 2021-12-21T21:45:57.262Z

I'll try that tomorrow :)

kevinc 2021-12-22T03:03:31.262500Z

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.

2021-12-21T06:47:17.248Z

https://gitlab.com/maximoburrito/advent2021/-/blob/main/src/day21/main.clj sloppy code as usual, but I think the approach is solid