diff options
author | Leah Neukirchen <leah@vuxu.org> | 2021-12-08 20:25:43 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2021-12-08 20:25:43 +0100 |
commit | 3f2351d8ca5ac29808808873c44b9f63a871ac8c (patch) | |
tree | 29b9251fdb8da2c587df1e96748b4de33c9e32a9 /day07.clj | |
parent | fa4b58b6ce7e011ee2b88e86648678c6bcd06e66 (diff) | |
download | adventofcode2021-3f2351d8ca5ac29808808873c44b9f63a871ac8c.tar.gz adventofcode2021-3f2351d8ca5ac29808808873c44b9f63a871ac8c.tar.xz adventofcode2021-3f2351d8ca5ac29808808873c44b9f63a871ac8c.zip |
day07
Diffstat (limited to 'day07.clj')
-rw-r--r-- | day07.clj | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/day07.clj b/day07.clj new file mode 100644 index 0000000..be8d66b --- /dev/null +++ b/day07.clj @@ -0,0 +1,37 @@ +(ns org.vuxu.aoc2021.day07 + (:require [clojure.string :as str])) + +(def data + (->> (str/split (str/trim-newline (slurp "day07")) #",") + (mapv parse-long))) + +(defn median [coll] + ; only works for even lengths but good enough here + (nth (sort coll) (/ (count coll) 2))) + +(defn integer-means [coll] + (let [mean (/ (apply + coll) (count coll))] + [(long (Math/floor mean)) + (long (Math/ceil mean))])) + +(defn abs [n] + (Math/abs n)) + +(defn fuel [n] + (* 1/2 n (inc n))) + +(def part1 + (->> data + (map (partial - (median data))) + (map abs) + (apply +))) +;; => 328187 + +(def part2 + (apply min (for [m (integer-means data)] + (->> data + (map (partial - m)) + (map abs) + (map fuel) + (apply +))))) +;; => 91257582N |