day 7
This commit is contained in:
parent
76a18ab740
commit
ff7fc15880
75
src/day7.clj
Normal file
75
src/day7.clj
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
(ns day7
|
||||||
|
(:require [clojure.string :as string]
|
||||||
|
[helpers :refer [get-input]]))
|
||||||
|
|
||||||
|
(def example
|
||||||
|
"190: 10 19
|
||||||
|
3267: 81 40 27
|
||||||
|
83: 17 5
|
||||||
|
156: 15 6
|
||||||
|
7290: 6 8 6 15
|
||||||
|
161011: 16 10 13
|
||||||
|
192: 17 8 14
|
||||||
|
21037: 9 7 18 13
|
||||||
|
292: 11 6 16 20")
|
||||||
|
|
||||||
|
(def input
|
||||||
|
(->>
|
||||||
|
;example
|
||||||
|
(get-input 7)
|
||||||
|
string/split-lines))
|
||||||
|
|
||||||
|
(defn permutation->operator [p]
|
||||||
|
(concat
|
||||||
|
(map #(if (even? %) + *) p)
|
||||||
|
(map #(if (odd? %) + *) p)))
|
||||||
|
|
||||||
|
(defn operators [n]
|
||||||
|
(let [add-operators
|
||||||
|
(fn [ops]
|
||||||
|
(vector (conj ops +)
|
||||||
|
(conj ops *)))]
|
||||||
|
(loop [i 1 ops [[+] [*]]]
|
||||||
|
(if (= n i)
|
||||||
|
ops
|
||||||
|
(recur (inc i)
|
||||||
|
(mapcat add-operators ops))))))
|
||||||
|
|
||||||
|
(defn test-calibration [ops-fn s]
|
||||||
|
(let [[tval ns] (string/split s #":")
|
||||||
|
tval (parse-long tval)
|
||||||
|
ns (vec (map parse-long
|
||||||
|
(-> ns string/trim (string/split #" "))))
|
||||||
|
ops (-> ns count dec ops-fn)
|
||||||
|
check-fn
|
||||||
|
(fn [op-seq]
|
||||||
|
(reduce
|
||||||
|
(fn [r i]
|
||||||
|
((get op-seq i) r (get ns (inc i))))
|
||||||
|
(first ns)
|
||||||
|
(range (count op-seq))))]
|
||||||
|
(if (empty?
|
||||||
|
(filter (partial = tval) (map check-fn ops)))
|
||||||
|
0 tval)))
|
||||||
|
|
||||||
|
(println "Total calibration result:"
|
||||||
|
(reduce + (map (partial test-calibration operators) input)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn || [a b]
|
||||||
|
(parse-long (str a b)))
|
||||||
|
|
||||||
|
(defn operators2 [n]
|
||||||
|
(let [add-operators
|
||||||
|
(fn [ops]
|
||||||
|
(vector (conj ops +)
|
||||||
|
(conj ops *)
|
||||||
|
(conj ops ||)))]
|
||||||
|
(loop [i 1 ops [[+] [*] [||]]]
|
||||||
|
(if (= n i)
|
||||||
|
ops
|
||||||
|
(recur (inc i)
|
||||||
|
(mapcat add-operators ops))))))
|
||||||
|
|
||||||
|
(println "Total calibration result (part 2):"
|
||||||
|
(reduce + (map (partial test-calibration operators2) input)))
|
||||||
Loading…
x
Reference in New Issue
Block a user