Skip to content

Instantly share code, notes, and snippets.

Created December 6, 2018 01:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Advent of Code 2018 - Day 5
(ns aoc.aoc5)
(defn polymer-drop [[c1 c2]]
(= c1 c2) false
(or (nil? c1) (nil? c2)) false
(= (Character/toLowerCase c1) (Character/toLowerCase c2)) true
:else false))
(defn shrink [input]
(loop [shrunk [] chars-to-test (take 2 input) left (drop 2 input)]
(and (empty? left) (every? nil? chars-to-test)) (apply str shrunk)
(nil? (first chars-to-test)) (recur shrunk [(last chars-to-test) (first left)] (rest left))
(polymer-drop chars-to-test) (if (empty? shrunk)
(recur shrunk (take 2 left) (drop 2 left))
(recur (pop shrunk) [(last shrunk) (first left)] (rest left)))
:else (recur (conj shrunk (first chars-to-test)) [(last chars-to-test) (first left)] (rest left)))))
(defn remove-char
"Remove all instances of a character (case-insensitive)
from a string"
[string chr]
(apply str (remove #(or (= % (Character/toUpperCase chr)) (= % (Character/toLowerCase chr))) string)))
(defn char-range
[start end]
(map char (range (int start) (inc (int end)))))
(defn find-shortest-polymer [input-string]
(apply min (pmap #(-> input-string
(remove-char %)
(count)) (char-range \a \z))))
(ns aoc.aoc5-test
(:require [aoc.aoc5 :as sut]
[clojure.test :refer [deftest testing is]]))
(deftest part-1-test
(testing "polarity"
(is (= (sut/polymer-drop [\A \A]) false))
(is (= (sut/polymer-drop [\a \a]) false))
(is (= (sut/polymer-drop [\a \b]) false))
(is (= (sut/polymer-drop [\a \A]) true))
(is (= (sut/polymer-drop [\A \a]) true))
(is (= (sut/polymer-drop [\c \C]) true)))
(testing "shrinking"
(is (= (sut/shrink "aAbBca") "ca"))
(is (= (sut/shrink "bbBa") "ba"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment