Created
December 6, 2018 01:41
-
-
Save ballpointcarrot/7e2e4d4dcb908612ecb0ef603b0b1b13 to your computer and use it in GitHub Desktop.
Advent of Code 2018 - Day 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns aoc.aoc5) | |
(defn polymer-drop [[c1 c2]] | |
(cond | |
(= 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)] | |
(cond | |
(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 %) | |
(shrink) | |
(count)) (char-range \a \z)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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