This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-21
Channels
- # adventofcode (27)
- # announcements (2)
- # babashka (1)
- # beginners (111)
- # calva (11)
- # cider (82)
- # clara (6)
- # clojure (44)
- # clojure-dev (5)
- # clojure-europe (27)
- # clojure-nl (5)
- # clojure-spec (3)
- # clojure-uk (3)
- # clojurescript (29)
- # core-async (5)
- # cursive (4)
- # datalevin (1)
- # datomic (39)
- # exercism (4)
- # figwheel-main (1)
- # fulcro (32)
- # graalvm (7)
- # gratitude (1)
- # integrant (4)
- # jobs (1)
- # lein-figwheel (3)
- # leiningen (4)
- # lsp (3)
- # luminus (3)
- # meander (2)
- # nextjournal (1)
- # off-topic (10)
- # other-languages (26)
- # pathom (14)
- # polylith (9)
- # re-frame (16)
- # remote-jobs (1)
- # shadow-cljs (4)
- # specter (2)
- # sql (6)
- # timbre (2)
- # tools-build (12)
- # xtdb (9)
🧵 Day 21 Solutions Thread: post your solution here
https://gitlab.com/maximoburrito/advent2021/-/blob/main/src/day21/main.clj sloppy code as usual, but I think the approach is solid
@U076FM90B 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 @U01HL2S0X71, but maybe it's my code?
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…)
@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
yeah, I did, but I prefer (->> coll (drop n …) first)
^_^
@U067R559Q 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)
@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]]
(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 :)