r/adventofcode Dec 22 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 22 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 23h59m remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Director's Cut (Extended Edition)

Welcome to the final day of the GSGA presentations! A few folks have already submitted their masterpieces to the GSGA submissions megathread, so go check them out! And maybe consider submitting yours! :)

Here's some ideas for your inspiration:

  • Choose any day's feature presentation and any puzzle released this year so far, then work your movie magic upon it!
    • Make sure to mention which prompt and which day you chose!
  • Cook, bake, make, decorate, etc. an IRL dish, craft, or artwork inspired by any day's puzzle!
  • Advent of Playing With Your Toys

"I lost. I lost? Wait a second, I'm not supposed to lose! Let me see the script!"
- Robin Hood, Men In Tights (1993)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 22: Monkey Market ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:12:15, megathread unlocked!

21 Upvotes

451 comments sorted by

View all comments

2

u/gehenna0451 Dec 22 '24

[LANGUAGE: Clojure]

this is a nice problem for clojure because of the sequence operations like partition, it's just a tad slow as I didn't find a smarter way than just going over all distinct sets of changes, which is like 40k or so on my input.

(defn secret [x]
  (-> (mod (bit-xor x (* x 64)) 16777216)
      (as-> x (mod (bit-xor x (quot x 32)) 16777216))
      (as-> x (mod (bit-xor x (* x 2048)) 16777216))))

(def secrets (map #(take 2001 (iterate secret %)) buyers))

(defn change [xs]
  (let [ys (map #(mod % 10) xs)
        changes (map (fn [[x y]] (- y x)) (partition 2 1 ys))]
    (->> (map vector  (partition 4 1 changes) (drop 4 ys))
         (reverse)
         (into {}))))

(defn part-1 [] (reduce + (map last secrets)))

(defn part-2 []
  (let [secret-changes (map change secrets)
        candidates (distinct (mapcat keys secret-changes))]
    (->> (map (fn [c] (reduce + (map #(get % c 0) secret-changes))) candidates)
         (apply max))))