do day 13
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
(ns day13
|
||||
(:require
|
||||
[clojure.string :as str]
|
||||
[core]
|
||||
[input-manager]))
|
||||
|
||||
(def lengths (->> (input-manager/get-input 2017 13)
|
||||
(map (comp #(mapv parse-long %)
|
||||
#(str/split % #": ")))
|
||||
(into {})))
|
||||
|
||||
(defn move-sensor [{:keys [idx depth increasing-depth] :as sensor}]
|
||||
(let [[keep-moving reverse-step] (if increasing-depth [inc dec] [dec inc])
|
||||
depth' (keep-moving depth)]
|
||||
(if (or (> depth' (dec (lengths idx)))
|
||||
(< depth' 0))
|
||||
(-> sensor
|
||||
(update :depth reverse-step)
|
||||
(update :increasing-depth not))
|
||||
(assoc sensor :depth depth'))))
|
||||
|
||||
(def initial-sensors (->> lengths
|
||||
keys
|
||||
(map #(hash-map :depth 0
|
||||
:idx %
|
||||
:increasing-depth true))))
|
||||
|
||||
(defn is-spotted-by-sensors [acc pos]
|
||||
(some #(when (and (= (:idx %) pos)
|
||||
(= (:depth %) 0))
|
||||
%)
|
||||
(:sensors acc)))
|
||||
|
||||
(->> lengths keys (apply max) inc range
|
||||
(reduce (fn [acc pos]
|
||||
(let [spotted (is-spotted-by-sensors acc pos)]
|
||||
(-> acc
|
||||
(update :sensors (partial map move-sensor))
|
||||
(update :solution + (if spotted
|
||||
(* pos (lengths pos))
|
||||
0)))))
|
||||
{:sensors initial-sensors
|
||||
:solution 0})
|
||||
:solution)
|
||||
|
||||
(loop [i 0
|
||||
start-val initial-sensors]
|
||||
(if
|
||||
(= true
|
||||
(->> lengths keys (apply max) inc range
|
||||
(reduce (fn [acc pos]
|
||||
(let [spotted (is-spotted-by-sensors acc pos)]
|
||||
(if spotted
|
||||
(reduced true)
|
||||
(update acc :sensors (partial map move-sensor)))))
|
||||
{:sensors start-val})))
|
||||
(recur (inc i) (map move-sensor start-val))
|
||||
i))
|
||||
Reference in New Issue
Block a user