golf
This commit is contained in:
parent
14ac947ae3
commit
e51b3296d6
@ -1,7 +1,7 @@
|
|||||||
(ns day10
|
(ns day10
|
||||||
(:require [clojure.math.combinatorics :as combo]
|
(:require [clojure.math.combinatorics :as combo]
|
||||||
[input-manager])
|
[input-manager])
|
||||||
(:import (com.microsoft.z3 BoolExpr Context Expr IntNum Optimize Optimize$Handle)))
|
(:import (com.microsoft.z3 Context Expr IntNum Optimize Optimize$Handle)))
|
||||||
|
|
||||||
(defn parse-line [line]
|
(defn parse-line [line]
|
||||||
{:indicators (->> line
|
{:indicators (->> line
|
||||||
@ -37,8 +37,8 @@
|
|||||||
(apply + (map minimum-button-mashing input))
|
(apply + (map minimum-button-mashing input))
|
||||||
|
|
||||||
;; part 2
|
;; part 2
|
||||||
(defn add-expressions [opt type expressions]
|
(defn add-expressions [opt expressions]
|
||||||
(.Add opt (into-array type expressions)))
|
(.Add opt (into-array Expr expressions)))
|
||||||
|
|
||||||
(->> input
|
(->> input
|
||||||
(map (fn [{:keys [joltages buttons]}]
|
(map (fn [{:keys [joltages buttons]}]
|
||||||
@ -52,25 +52,22 @@
|
|||||||
count
|
count
|
||||||
range
|
range
|
||||||
(map #(.mkIntConst ctx (str "j" %))))]
|
(map #(.mkIntConst ctx (str "j" %))))]
|
||||||
(add-expressions opt BoolExpr
|
(add-expressions opt (for [idx (range (count joltages))]
|
||||||
(for [idx (range (count joltages))]
|
(let [jn (nth js idx)
|
||||||
(let [jn (nth js idx)
|
summation-args (->> buttons
|
||||||
summation-args (->> buttons
|
(map set)
|
||||||
(map set)
|
(map-indexed vector)
|
||||||
(map-indexed vector)
|
(filter #(contains? (second %) idx))
|
||||||
(filter #(contains? (second %) idx))
|
(map first)
|
||||||
(map first)
|
(map #(nth bs %))
|
||||||
(map #(nth bs %))
|
(into-array Expr)
|
||||||
(into-array Expr)
|
(.mkAdd ctx))]
|
||||||
(.mkAdd ctx))]
|
(.mkEq ctx jn summation-args))))
|
||||||
(.mkEq ctx jn summation-args))))
|
(add-expressions opt (for [[idx joltage] (map-indexed vector joltages)]
|
||||||
(add-expressions opt Expr
|
(let [jn (nth js idx)]
|
||||||
(for [[idx joltage] (map-indexed vector joltages)]
|
(.mkEq ctx jn (.mkInt ctx (long joltage))))))
|
||||||
(let [jn (nth js idx)]
|
(add-expressions opt (for [b bs]
|
||||||
(.mkEq ctx jn (.mkInt ctx (long joltage))))))
|
(.mkGe ctx b (.mkInt ctx (long 0)))))
|
||||||
(add-expressions opt 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 (.getValue mx))))))
|
(.getInt ^IntNum (.getValue mx))))))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user