r/adventofcode • • Dec 08 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 8 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's theme ingredient is… *whips off cloth covering and gestures grandly*

International Ingredients

A little je ne sais quoi keeps the mystery alive. Try something new and delight us with it!

  • Code in a foreign language

    • Written or programming, up to you!
    • If you don’t know any, Swedish Chef or even pig latin will do
  • Test your language’s support for Unicode and/or emojis
  • Visualizations using Unicode and/or emojis are always lovely to see

ALLEZ CUISINE!

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


--- Day 8: Haunted Wasteland ---


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:10:16, megathread unlocked!

52 Upvotes

968 comments sorted by

View all comments

1

u/[deleted] Dec 08 '23 edited Dec 08 '23

[LANGUAGE: Clojure]

Wrote a quick and dirty LCM calculator for part 2. I could have used a Java interop for the GCD but yolo.

(defn- parse-node
  [line]
  (let [[node elements] (string/split line #" = ")
        [left right] (-> elements
                         (string/replace  #"[\(\),]" "")
                         (string/split #" "))]
    {(keyword node) {:L (keyword left), :R (keyword right)}}))


(defn- parse-input
  [input]
  (let [[instructions nodes] (string/split input #"\n\n")
        instructions (map #(-> % str keyword) instructions)
        nodes (->> (string/split-lines nodes)
                   (map parse-node)
                   (into {}))]
    [instructions nodes]))


(defn- get-steps
  [nodes instructions current-node break-fn]
  (loop [steps 0
         current-node current-node
         instructions (cycle instructions)]
    (if (break-fn current-node)
      steps
      (recur (inc steps)
             (let [instruction (first instructions)]
               (-> nodes current-node instruction))
             (rest instructions)))))


(defn- gcd [a b]
  (loop [a a
         b b]
    (if (zero? b)
      a
      (recur b (mod a b)))))


(defn- lcm
  ([a b]
   (-> b
       (/ (gcd a b))
       (* a)))
  ([nums]
   (reduce #(lcm %1 %2) nums)))


(comment
  ;; part 1
  (let [[instructions nodes] (parse-input input)]
    (get-steps nodes instructions :AAA #(= :ZZZ %)))

  ;; part 2
  (let [[instructions nodes] (parse-input input)]
    (->> nodes
         (reduce (fn [res node]
                   (if (-> node first name str last (= \A))
                     (conj res (first node))
                     res)) [])
         (map (fn [node]
                (get-steps nodes instructions node #(-> % name str last (= \Z)))))
         (lcm)))
  )

1

u/daggerdragon Dec 08 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.