-
-
Save MeikeMertsch/6a14074aa336f4d377c5d0fb6a01ca2b to your computer and use it in GitHub Desktop.
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 christmas.day07) | |
(defn find-candidates [remaining restrictions] | |
(->> (map last restrictions) | |
distinct | |
(reduce #(remove (fn [x] (= %2 x)) %1) remaining) | |
sort)) | |
(defn find-next [remaining restrictions] | |
(first (find-candidates remaining restrictions))) | |
(defn exercise07 [allofthem file] | |
(loop [remaining allofthem | |
result [] | |
restrictions file] | |
(let [newestResult (find-next remaining restrictions)] | |
(if (empty? remaining) | |
(apply str result) | |
(recur (remove #(= % newestResult) remaining) | |
(conj result newestResult) | |
(remove #(= (first %) newestResult) restrictions)))))) | |
;------------- Part II | |
(defn duration [masterpiece secs] | |
(- (int (first masterpiece)) (- 64 secs))) | |
(defn tick [workers] | |
(reduce #(assoc %1 (first %2) (dec (last %2))) workers workers)) | |
(defn free-up-workers [workers finishedwork] | |
(apply dissoc workers finishedwork)) | |
(defn finish-work [workers] | |
(map first (filter #(= 1 (val %)) workers))) | |
(defn time-candidates [candidates secs] | |
(map #(vector % (duration % secs)) candidates)) | |
(defn get-to-work [elves workers timedcandidates] | |
(->> (- elves (count workers)) | |
(#(take % timedcandidates)) | |
(reduce #(apply assoc %1 %2) workers))) | |
(defn tcandidates [remaining restrictions secs] | |
(->> (find-candidates remaining restrictions) | |
(#(time-candidates % secs)))) | |
(defn loosen-restrictions [restrictions finishedwork] | |
(reduce #(remove (fn [x] (= %2 (first x))) %1) restrictions finishedwork)) | |
(defn throw-out-finished [remaining finishedwork newworkers] | |
(->> (apply disj remaining finishedwork) | |
(into #{}) | |
(#(apply disj % newworkers)))) | |
(defn thingy [[elves restrictions workers remaining secs] newtime] | |
(let [finishedwork (finish-work workers) | |
newrestrictions (loosen-restrictions restrictions finishedwork) | |
newremainders (throw-out-finished remaining finishedwork (keys workers)) | |
newworkers (->> (tick workers) | |
(#(free-up-workers % finishedwork)) | |
(#(get-to-work elves % (tcandidates newremainders newrestrictions secs))))] | |
(vector elves | |
newrestrictions | |
newworkers | |
newremainders | |
secs))) | |
(defn exercise07b [restrictions elves remaining secs] | |
(->> (range) | |
(reductions thingy [elves restrictions {} remaining secs]) | |
(drop 1) | |
(take-while #(not (empty? (nth % 2)))) | |
count)) |
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 christmas.day07-test | |
(:require [christmas.day07 :as chr] | |
[expectations :refer :all] | |
[clojure.string :as str])) | |
(defn getFile [fileName] | |
(->> (slurp fileName) | |
(clojure.string/split-lines) | |
(map #(str/replace % (re-pattern "Step ") "")) | |
(map #(str/replace % (re-pattern " can begin.") "")) | |
(map #(str/split % (re-pattern " must be finished before step "))))) | |
(def allofthem ["A" "B" "C" "D" "E" "F"]) | |
(def realAllofthem ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]) | |
(def realFile (getFile "resources/Input07real")) ; TestData from the description | |
(def file (getFile "resources/Input07")) ; Input Data. I removed the newline at the end. | |
(expect ["A" "B" "D" "E" "H" "I" "J" "K" "L" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"] | |
(sort (vec (into #{} (map last realFile))))) | |
(expect "C" (chr/find-next ["A" "B" "C" "D" "E" "F"] file)) | |
(expect "CABDFE" (chr/exercise07 allofthem file)) | |
(expect "CFGHAEMNBPRDISVWQUZJYTKLOX" (chr/exercise07 realAllofthem realFile)) | |
;--------------------- | |
(expect 15 (chr/exercise07b file 2 allofthem 0)) | |
(expect 828 (chr/exercise07b realFile 5 realAllofthem 60)) | |
(expect 5 (chr/duration "E" 0)) | |
(expect 26 (chr/duration "Z" 0)) | |
(expect 86 (chr/duration "Z" 60)) | |
(expect {"A" 0 "Z" 25} (chr/tick {"A" 1 "Z" 26})) | |
(expect {"C" 2 "N" 13} (chr/tick {"C" 3 "N" 14})) | |
(expect {"Z" 25} (chr/free-up-workers {"A" 0 "Z" 25} ["A"])) | |
(expect {} (chr/free-up-workers {"A" 0 "Z" 0} ["A" "Z"])) | |
(expect ["A"] (chr/finish-work {"A" 1 "Z" 25})) | |
(expect ["A" "B"] (chr/finish-work {"A" 1 "C" 2 "B" 1 "Z" 25})) | |
(expect [["A" 1] ["N" 14] ["K" 11]] (chr/time-candidates ["A" "N" "K"] 0)) | |
(expect {"B" 2 "Z" 25 "A" 1 "N" 14 "K" 11} (chr/get-to-work 5 {"B" 2 "Z" 25} [["A" 1] ["N" 14] ["K" 11]])) | |
(expect {"B" 2 "Z" 25 "A" 1 } (chr/get-to-work 3 {"B" 2 "Z" 25} [["A" 1] ["N" 14] ["K" 11]])) | |
(expect {"A" 2 "G" 25 "N" 14} (chr/get-to-work 5 {"A" 2 "G" 25} [["N" 14]])) | |
(expect {"A" 2 "G" 25} (chr/get-to-work 5 {"A" 2 "G" 25} [])) | |
(expect {"B" 2 "Z" 25 "A" 1 "N" 14 "K" 11} (chr/get-to-work 5 {"B" 2 "Z" 25 "A" 1 "N" 14 "K" 11} [["Q" 1] ["R" 14] ["S" 11]])) | |
(expect [["C" 3]] (chr/tcandidates allofthem file 0)) | |
(expect (drop 2 file) (chr/loosen-restrictions file ["C"])) | |
(expect #{"A" "B" "E" "F"} (chr/throw-out-finished (into #{} allofthem) ["C" "D"] {})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment