day 6
This commit is contained in:
parent
b704812923
commit
e15e01a9a5
53
src/day6.clj
Normal file
53
src/day6.clj
Normal file
@ -0,0 +1,53 @@
|
||||
(ns day6
|
||||
(:require [clojure.edn :as edn]
|
||||
[clojure.string :as string]
|
||||
[helpers :refer [get-input]]))
|
||||
|
||||
(def example
|
||||
"123 328 51 64
|
||||
45 64 387 23
|
||||
6 98 215 314
|
||||
* + * + ")
|
||||
|
||||
;;(def input (string/split-lines example))
|
||||
(def input (string/split-lines (get-input 6)))
|
||||
|
||||
(defn calc [& args]
|
||||
(let [v (mapv edn/read-string args)
|
||||
op (peek v)
|
||||
ns (pop v)]
|
||||
(apply (resolve op) ns)))
|
||||
|
||||
(println "Total sum:"
|
||||
(->>
|
||||
input
|
||||
(mapv #(string/split (string/trim %) #"\ +"))
|
||||
(apply map calc)
|
||||
(reduce +)))
|
||||
|
||||
(def nums (mapv vec (pop input)))
|
||||
(def ops (mapv (comp resolve edn/read-string)
|
||||
(string/split (string/trim (peek input)) #"\ +")))
|
||||
|
||||
(defn get-ns [lines]
|
||||
(loop [ns []
|
||||
lines lines]
|
||||
(if (empty? (first lines))
|
||||
[ns []]
|
||||
(let [n (->> (mapv peek lines)
|
||||
(apply str)
|
||||
string/trim)]
|
||||
(if (zero? (count n))
|
||||
[ns (mapv pop lines)]
|
||||
(recur (conj ns (edn/read-string n))
|
||||
(mapv pop lines)))))))
|
||||
|
||||
(println "Result (part 2):"
|
||||
(loop
|
||||
[lines nums
|
||||
ops ops
|
||||
rs []]
|
||||
(if (empty? ops)
|
||||
(reduce + rs)
|
||||
(let [[ns lines] (get-ns lines)]
|
||||
(recur lines (pop ops) (conj rs (apply (peek ops) ns)))))))
|
||||
Loading…
x
Reference in New Issue
Block a user