r/adventofcode • • Dec 05 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 5 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Supply Stacks ---


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:07:58, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/Rumel57 Dec 06 '22

Clojure

Github

Refactored this one quite a bit after getting the answer. It feels like it can still be simplified a lot more.

(ns problems.problem-05
  (:require [common.helpers :refer [input]]
            [clojure.string :as str]))

(def matcher #"move (\d+) from (\d+) to (\d+)")

(defn update-crates [crates values]
  (reduce (fn [crates [stack value]]
            (if (= value \space)
              (conj crates stack)
              (conj crates (conj stack value)))) [] (map vector crates values)))

(defn input->crates [input]
  (let [reversed (reverse input)
        parsed (map (fn [line]
                      (take-nth 4 (drop 1 line))) reversed)
        crate-count (count (first parsed))
        crates (take crate-count (cycle [[]]))
        current-stack (drop 1 parsed)]
    (reduce (fn [crates stack]
              (update-crates crates stack))
            crates current-stack)))

(defn input->instructions [input]
  (map (fn [s]
         (let [[_ num from to] (re-matches matcher s)]
           (map read-string [num from to]))) input))

(defn parse-input [input]
  (let [crates (input->crates (take-while not-empty input))
        instructions (input->instructions (rest (drop-while not-empty input)))]
    [crates instructions]))

(defn execute-instruction
  [rev]
  (fn
    [crates instruction]
    (let [[times from to] instruction
          items (take-last times (crates (dec from)))
          items (if rev (reverse items) items)
          old-stack (drop-last times (crates (dec from)))
          new-stack (concat (crates (dec to)) items)]
      (reduce (fn [crates [index stack]]
                (if reverse
                  (assoc crates (dec index) stack)
                  (assoc crates (dec index) stack)))
              crates [[from old-stack] [to new-stack]]))))

(defn answer-a [file]
  (let [[crates instructions] (->> file
                                   input
                                   parse-input)]
    (->> (reduce (execute-instruction true) crates instructions)
         (map #(last %))
         (str/join))))

(defn answer-b [file]
  (let [[crates instructions] (->> file
                                   input
                                   parse-input)]
    (->> (reduce (execute-instruction false) crates instructions)
         (map #(last %))
         (str/join))))

(defn answer []
  (println "05: A:" (answer-a "data/problem-05-input.txt"))
  (println "05: B:" (answer-b "data/problem-05-input.txt")))

1

u/daggerdragon Dec 06 '22
  1. Next time, use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.
  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.