X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=04-08-amidakuji%2Fday4.clj;fp=04-08-amidakuji%2Fday4.clj;h=3c5d49409af1186c7b6512b3c865320e0cb03a50;hb=da956e6e409278cc6e3250254912ed31ea0d1b71;hp=0000000000000000000000000000000000000000;hpb=c78bf218fb6d75d478fef09b8067a23332d4de45;p=ou-summer-of-code-2017.git diff --git a/04-08-amidakuji/day4.clj b/04-08-amidakuji/day4.clj new file mode 100644 index 0000000..3c5d494 --- /dev/null +++ b/04-08-amidakuji/day4.clj @@ -0,0 +1,70 @@ +(ns day4 + (:require [clojure.string :as str])) + +(defstruct link :height :left :right) + +; (def network (str/split-lines (slurp "../04-small.txt"))) +(def network (str/split-lines (slurp "../04-lines.txt"))) + +(defn parse-link [l, h] + (let [endss (rest (str/split l #"\D+")) + ends (map #(Integer/parseInt %) endss)] + (struct-map link + :height h + :left (first ends) + :right (second ends)))) + +(defn parse-network [links] + (map-indexed + (fn [i l] (parse-link l i)) + links)) + +(def parsed-network + (parse-network network)) + + +(def initial + (str/split "abcdefghijklmnopqrstuvwxyz" #"")) + +(defn apply-link [items link] + (let [li (get link :left) + ri (get link :right) + le (nth items li) + re (nth items ri) + pre (subvec items 0 li) + mid (subvec items (inc li) ri) + suf (subvec items (inc ri))] + (into [] (concat pre (vector re) mid (vector le) suf)))) + +(defn follow [items links] + (reduce apply-link items links)) + + +(defn lane-count [links] + (inc (apply max (map (fn [l] (get l :right)) links)))) + +(defn initial-heights [links] + (vec (replicate (lane-count links) -1))) + +(defn pack-one [lane-heights link] + (let + [li (get link :left) + ri (get link :right) + new-height (inc (max (nth lane-heights li) + (nth lane-heights ri)))] + (assoc lane-heights li new-height ri new-height))) + +(defn pack [lane-heights links] + (reduce pack-one lane-heights links)) + + +(spit "day4.out" (prn-str + (str/join (follow initial parsed-network)))) + +(spit "day4.out" (prn-str + (apply max + (pack + (initial-heights parsed-network) + parsed-network))) + :append true) +