Compare commits
2 Commits
1087a7649b
...
9703c017a9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9703c017a9 | ||
|
|
733c833f66 |
@@ -0,0 +1,67 @@
|
||||
(ns day07
|
||||
(:require
|
||||
[clojure.string :as str]
|
||||
[core :refer [split-whitespace]]
|
||||
[input-manager]))
|
||||
|
||||
(def input
|
||||
(->> (input-manager/get-input-raw 2017 7)
|
||||
str/split-lines
|
||||
(map (comp split-whitespace
|
||||
#(str/replace % #"," "")))))
|
||||
|
||||
;; part 1
|
||||
(def base-node
|
||||
(->> input
|
||||
(filter (fn [[_ _ _ & children]] children))
|
||||
(filter (fn [[node]]
|
||||
(empty? (filter (fn [[_ _ _ & children]]
|
||||
(some #(= % node) children))
|
||||
input))))
|
||||
ffirst))
|
||||
|
||||
(do
|
||||
(def get-depth
|
||||
(memoize (fn [node]
|
||||
(let [[_ w-str _ & children] (first (filter (fn [[n]] (= node n))
|
||||
input))
|
||||
w (parse-long (subs w-str 1 (dec (count w-str))))]
|
||||
(apply + w (map get-depth children))))))
|
||||
(def get-weight
|
||||
(memoize (fn [node]
|
||||
(let [w-str (->> input
|
||||
(filter (fn [[n]] (= node n)))
|
||||
first
|
||||
second)]
|
||||
(parse-long (subs w-str 1 (dec (count w-str))))))))
|
||||
(def get-children
|
||||
(memoize (fn [node] (->> input
|
||||
(filter (fn [[n]] (= node n)))
|
||||
first
|
||||
(drop 3))))))
|
||||
|
||||
(defn get-children-data [node]
|
||||
(map (juxt identity get-depth get-weight) (get-children node)))
|
||||
|
||||
(defn get-counts [data]
|
||||
(->> data
|
||||
(map second)
|
||||
frequencies
|
||||
(sort-by second)))
|
||||
|
||||
;; part 2
|
||||
(let [[bad-parent bad-node]
|
||||
(loop [parent nil
|
||||
node base-node]
|
||||
(let [data (get-children-data node)
|
||||
counts (get-counts data)
|
||||
row (first (filter #(= (second %) (ffirst counts)) data))]
|
||||
(if (= (count counts) 1)
|
||||
[parent node]
|
||||
(recur node (first row)))))
|
||||
diff (->> bad-parent
|
||||
get-children-data
|
||||
get-counts
|
||||
(map first)
|
||||
(apply -))]
|
||||
(- (get-weight bad-node) diff))
|
||||
@@ -0,0 +1,46 @@
|
||||
(ns day08
|
||||
(:require
|
||||
[clojure.string :as str]
|
||||
[core]
|
||||
[input-manager]))
|
||||
|
||||
(def op {"inc" +
|
||||
"dec" -})
|
||||
(def cond-op {"<" <
|
||||
"<=" <=
|
||||
">" >
|
||||
">=" >=
|
||||
"==" =
|
||||
"!=" not=})
|
||||
(def input (map (comp
|
||||
(fn [[name operation num _ cond-name cond-operation cond-val]]
|
||||
[name
|
||||
(op operation)
|
||||
(parse-long num)
|
||||
cond-name
|
||||
(cond-op cond-operation)
|
||||
(parse-long cond-val)])
|
||||
core/split-whitespace)
|
||||
(input-manager/get-input 2017 8)))
|
||||
|
||||
(defn compute [acc [reg reg-op val cond-reg cond-op cond-val]]
|
||||
(if (cond-op (acc cond-reg 0) cond-val)
|
||||
(assoc acc reg (reg-op (acc reg 0) val))
|
||||
acc))
|
||||
|
||||
;; part 1
|
||||
(->> input
|
||||
(reduce compute {})
|
||||
(sort-by second)
|
||||
last
|
||||
second)
|
||||
|
||||
;; part 2
|
||||
(->> input
|
||||
(reductions compute {})
|
||||
(mapcat identity)
|
||||
set
|
||||
(sort-by second)
|
||||
last
|
||||
second)
|
||||
|
||||
Reference in New Issue
Block a user