Skip to content

Instantly share code, notes, and snippets.

@MeikeMertsch
Last active December 8, 2018 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeikeMertsch/f1f3a1bfc157c3ed3a6cfcb1dec0ff57 to your computer and use it in GitHub Desktop.
Save MeikeMertsch/f1f3a1bfc157c3ed3a6cfcb1dec0ff57 to your computer and use it in GitHub Desktop.
AoC_Day_08.clj
(ns christmas.day08)
(defn deal-with-0 [input]
(let [candidate (last (split-with #(not= 0 %) input))]
(if (= (+ 2 (second candidate)) (count candidate))
(concat (drop-last (count candidate) input)
[candidate])
input)))
(defn hasNoSeq? [candidates]
(or (= (count candidates) 1)
(even? (count candidates))))
(defn seq-cand [input]
(let [temp (take-last 3 (partition-by class input))
cand (vector (take-last 2 (first temp)) (second temp) (last temp))
count-seq (first (butlast (first cand)))
count-num (last (first cand))]
;(println cand (first cand) count-num)
(if (and (= count-seq (count (second cand)))
(= count-num (count (last cand))))
(concat (drop-last (count (apply concat cand)) input)
[(apply concat cand)])
input)))
(defn deal-with-seq [input]
(let [candidates (partition-by class input)]
(if (hasNoSeq? candidates)
input
(seq-cand input))))
(defn check [input]
(cond
(contains? (into #{} (butlast input)) 0) (deal-with-0 input)
(not (hasNoSeq? (partition-by class input))) (seq-cand input)
:else input))
(defn build-tree [input number]
(check (concat input [number])))
(defn exercise08 [input]
(->> (reduce build-tree [] input)
last
(tree-seq coll? #(rest (rest %)))
(remove coll?)
(apply +)))
(defn something-smart [x y]
(<= y (count (second x))))
(defn node-has-no-kids? [my_node]
(= 0 (first my_node)))
(defn node-without-kids [my_node]
(apply + (nnext my_node)))
(defn node-with-kids [my_node]
(let [parts (partition-by class my_node)]
(->> (filter #(something-smart parts %) (last parts))
(map #(nth (second parts) (dec %)))
(#(map (fn [x] (if (node-has-no-kids? x)
(node-without-kids x)
(node-with-kids x))) %))
(apply +))))
(defn exercise08b [input]
(->> (reduce build-tree [] input)
last
node-with-kids))
(ns christmas.day08-test
(:require [christmas.day08 :as chr]
[expectations :refer :all]
[christmas.core :refer :all]
[clojure.string :as str]))
(defn parse [my_string]
(int (bigint my_string)))
(defn getFile [fileName]
(-> (slurp fileName)
(str/split #" ")
(#(map parse %))))
(def file (getFile "resources/Input08"))
(def fileb (getFile "resources/Input08b")) ; I invented that one
(def realFile (getFile "resources/Input08real"))
(expect 66 (chr/exercise08b file))
(expect 17 (chr/exercise08b fileb))
(expect 16653 (chr/exercise08b realFile))
(expect 99 (chr/node-without-kids [0 1 99]))
(expect 33 (chr/node-without-kids [0 3 10 11 12]))
(expect 0 (chr/node-with-kids [1 1 [0 1 99] 2]))
(expect 66 (chr/node-with-kids [2 3 [0 3 10 11 12] [1 1 [0 1 99] 2] 1 1 2]))
(expect 17 (chr/node-with-kids [2 1 [3 3 [0 1 2] [0 3 7 8 9] [1 1 [0 2 6 12] 1] 4 1 2] [1 3 [0 1 17] 1 4 4] 2]))
(expect 17 (chr/node-with-kids [1 3 [0 1 17] 1 4 4]))
(expect 17 (chr/node-without-kids [0 1 17]))
;_________________
(expect 138 (chr/exercise08 file))
(expect 80 (chr/exercise08 fileb))
(expect 41555 (chr/exercise08 realFile))
(expect [2] (chr/build-tree [] 2))
(expect [2 1 3 3 0] (chr/build-tree [2 1 3 3] 0))
(expect [2 1 3 3 [0 1 2]] (chr/build-tree [2 1 3 3 0 1] 2))
(expect [2 1 3 3 0 1] (chr/build-tree [2 1 3 3 0] 1))
(expect [2 3 [0 3 10 11 12] 1] (chr/build-tree [2 3 [0 3 10 11 12]] 1))
(expect [2 3 [0 3 10 11 12] 1 1 [0 1 99]] (chr/build-tree [2 3 [0 3 10 11 12] 1 1 0 1] 99))
(expect [2 1 3 3 [0 1 2]] (chr/check [2 1 3 3 0 1 2]))
(expect [2 1 3 3 0 1] (chr/check [2 1 3 3 0 1]))
(expect [2 1 3 3 [0 1 2]] (chr/deal-with-0 [2 1 3 3 0 1 2]))
(expect [2 1 3 3 0 3] (chr/deal-with-0 [2 1 3 3 0 3]))
(expect [2 1 3 3 [1 1 [] 2]] (chr/check [2 1 3 3 1 1 [] 2]))
(expect [2 1 3 1 1 []] (chr/check [2 1 3 1 1 []]))
(expect [2 1 [] 3 2 1 [] 1] (chr/check [2 1 [] 3 2 1 [] 1]))
(expect [2 1 3 2 1 1] (chr/check [2 1 3 2 1 1]))
(expect [2 1 [3 1 [] [] [] 2]] (chr/check [2 1 3 1 [] [] [] 2]))
2 1 3 3 0 1 2 0 3 7 8 9 1 1 0 2 6 12 1 4 1 2 1 3 0 1 17 1 4 4 2
@MeikeMertsch
Copy link
Author

Revision 1: Test run in ~ 2300 msecs

@MeikeMertsch
Copy link
Author

Revision 2: Tests run in ~600 msecs

@MeikeMertsch
Copy link
Author

Revision 3: Tests run in ~ 800 msecs

@MeikeMertsch
Copy link
Author

MeikeMertsch commented Dec 8, 2018

Revision 4: Tests run in ~40000 msecs
mind blown!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment