do day 13

This commit is contained in:
Adam Jeniski
2026-05-15 09:27:49 -04:00
parent a1db8ae128
commit b89c7ce3f2
+58
View File
@@ -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))