-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.clj
71 lines (57 loc) · 1.57 KB
/
common.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns aoc.common
(:require [clojure.java.io :refer [file]]
[clojure.pprint :refer [pprint]]
[clojure.string :as str]))
(defn read-input [& {:keys [split-with test use-test] :or {split-with #"\n" test nil use-test true}}]
(->
(if (and use-test (some? test))
test
(-> *file*
file
.getParent
(file "input.txt")
slurp))
(str/split split-with)))
(defn split-to-ints [s]
(->> s
(re-seq #"-?\d+")
(map parse-long)))
(defn count-where [fn coll]
(count (filter fn coll)))
(defn zip [& lists]
(apply map (conj lists vector)))
(defn reduce-right
([f v vs]
(loop [v v vs vs]
(if (empty? vs)
v
(recur (f (first vs) v) (rest vs)))))
([f [v & vs]]
(reduce-right f v vs)))
(defn sum [& lists]
(apply + 0 (filter some? (flatten lists))))
(defn spy [v] (pprint v) v)
(defn re-seq-indexed [pattern string]
(let [m (re-matcher pattern string)]
((fn step []
(when (. m find)
(cons {:match (. m group)
:start (. m start)
:end (. m end)} (lazy-seq (step))))))))
(defn take-until [pred coll]
(lazy-seq
(when-let [[f & r] (seq coll)]
(if (pred f)
[f]
(cons f (take-until pred r))))))
(defn numeric? [s] (re-matches #"\-?\d+" s))
(defn find-index [f coll]
(->> coll (keep-indexed #(when (f %1 %2) %1)) first))
(defn tee [fs val]
(map #(% val) fs))
(defn inclusive-range [a b]
(range (min a b) (inc (max a b))))
(defn manhattan [p1 p2]
(->> (zip p1 p2)
(map (fn [[v1 v2]] (abs (- v1 v2))))
sum))