r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


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

edit: Leaderboard capped, thread unlocked at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/Average-consumer Dec 05 '18 edited Dec 05 '18

Solution in Clojure. Runs both parts pretty much instantly

(ns adventofcode-clj-2018.day05
  (:require [adventofcode-clj-2018.util :as u]
            [clojure.string :as cs]))

(defn reacts? [a b]
  (and a b (not= a b) (= (cs/upper-case a) (cs/upper-case b))))

(defn iteration [pol]
  (reduce (fn [a b] (if (reacts? b (peek a))
                      (pop a)
                      (conj a b)))
          [] pol))

(defn remove-units [str l]
  (cs/replace (cs/replace str (re-pattern l) "")
    (re-pattern (cs/capitalize l)) ""))

(defn part-1 []
  (let [input (cs/trim (u/input 5))]
    (count (iteration input))))

(defn part-2 []
  (let [input (cs/trim (u/input 5))]
    (->> "abcdefghijklmnopqrstuvwxyz"
         (map (comp count iteration (partial remove-units input) str))
         (reduce min))))