diff options
author | Leah Neukirchen <leah@vuxu.org> | 2021-12-06 14:53:44 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2021-12-06 14:53:44 +0100 |
commit | 7b5dad8d4c9d4f91fc13f950ceff75383b75c5c4 (patch) | |
tree | 188a490c63b39a560ba4f7056979eaa3eebda7a6 /day05.clj | |
parent | e9bdceb4c8f0322b4711811ec689f1ea3472a089 (diff) | |
download | adventofcode2021-7b5dad8d4c9d4f91fc13f950ceff75383b75c5c4.tar.gz adventofcode2021-7b5dad8d4c9d4f91fc13f950ceff75383b75c5c4.tar.xz adventofcode2021-7b5dad8d4c9d4f91fc13f950ceff75383b75c5c4.zip |
day05
Diffstat (limited to 'day05.clj')
-rw-r--r-- | day05.clj | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/day05.clj b/day05.clj new file mode 100644 index 0000000..6e5185b --- /dev/null +++ b/day05.clj @@ -0,0 +1,44 @@ +(ns org.vuxu.aoc2021.day05 + (:require [clojure.string :as str])) + +(def data + (->> (slurp "day05") + (str/split-lines) + (map #(str/split % #",| -> ")) + (map (partial map parse-long)))) + +(use 'criterium.core) + +(def part1 + (->> data + (filter (fn [[x1 y1 x2 y2]] + (or (= x1 x2) (= y1 y2)))) + (mapcat (fn [[x1 y1 x2 y2]] + (for [x (range (min x1 x2) (inc (max x1 x2))) + y (range (min y1 y2) (inc (max y1 y2)))] + [x y]))) + frequencies + vals + (filter #(< 1 %)) + count)) +;; => 5092 + +(defn points [[x1 y1 x2 y2]] + (let [dx (Long/signum (- x2 x1)) + dy (Long/signum (- y2 y1))] + (loop [x x1 + y y1 + r []] + (if (and (= x2 x) + (= y2 y)) + (conj r [x y]) + (recur (+ x dx) (+ y dy) (conj r [x y])))))) + +(def part2 + (->> data + (mapcat points) + frequencies + vals + (filter #(< 1 %)) + count)) +;; => 20484 |