This commit is contained in:
Adam Jeniski 2025-12-22 18:00:33 -05:00
parent 8fa11862e1
commit 14ac947ae3

View File

@ -37,6 +37,9 @@
(apply + (map minimum-button-mashing input)) (apply + (map minimum-button-mashing input))
;; part 2 ;; part 2
(defn add-expressions [opt type expressions]
(.Add opt (into-array type expressions)))
(->> input (->> input
(map (fn [{:keys [joltages buttons]}] (map (fn [{:keys [joltages buttons]}]
(let [ctx (Context.) (let [ctx (Context.)
@ -49,35 +52,26 @@
count count
range range
(map #(.mkIntConst ctx (str "j" %))))] (map #(.mkIntConst ctx (str "j" %))))]
(.Add (add-expressions opt BoolExpr
opt (for [idx (range (count joltages))]
(into-array (let [jn (nth js idx)
BoolExpr summation-args (->> buttons
(for [idx (range (count joltages))] (map set)
(let [jn (nth js idx) (map-indexed vector)
summation-args (->> buttons (filter #(contains? (second %) idx))
(map set) (map first)
(map-indexed vector) (map #(nth bs %))
(filter #(contains? (second %) idx)) (into-array Expr)
(map first) (.mkAdd ctx))]
(map #(nth bs %)) (.mkEq ctx jn summation-args))))
(into-array Expr) (add-expressions opt Expr
(.mkAdd ctx))] (for [[idx joltage] (map-indexed vector joltages)]
(.mkEq ctx jn summation-args))))) (let [jn (nth js idx)]
(.Add (.mkEq ctx jn (.mkInt ctx (long joltage))))))
opt (add-expressions opt Expr
(into-array (for [b bs]
Expr (.mkGe ctx b (.mkInt ctx (long 0)))))
(for [[idx joltage] (map-indexed vector joltages)]
(let [jn (nth js idx)]
(.mkEq ctx jn (.mkInt ctx (long joltage)))))))
(.Add
opt
(into-array
Expr
(for [b bs]
(.mkGe ctx b (.mkInt ctx (long 0))))))
(let [^Optimize$Handle mx (.MkMinimize opt (.mkAdd ctx (into-array Expr bs)))] (let [^Optimize$Handle mx (.MkMinimize opt (.mkAdd ctx (into-array Expr bs)))]
(.Check opt (into-array Expr [])) (.Check opt (into-array Expr []))
(.getInt ^IntNum (.getLower mx)))))) (.getInt ^IntNum (.getValue mx))))))
(apply +)) (apply +))