aboutsummaryrefslogtreecommitdiff
path: root/challenge-131/tyler-wardhaugh/clojure/src/tw/weekly/c131/t2.clj
blob: 81f42897c98155b02938631d5f7aafdb2469a88f (plain)
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
(ns tw.weekly.c131.t2
  (:require [clojure.pprint :refer [cl-format]]))

;;;
; Task description for TASK #2, Find Pairs
;;;
(def DEFAULT-INPUT ["\"\"[]()" "\"I like (parens) and the Apple ][+\" they said."])

(defn re-quote-set
  "Turn a sequence of characters (either a string or a collection) into a regex
  matching that set."
  [coll]
  (re-pattern (cl-format nil "[\\Q~{~a~}\\E]" coll)))

(defn split-alternate
  "Split a collection into two collections by alternating the elements."
  [coll]
  (-> (fn [[a b] i v] (if (even? i) [(conj a v) b] [a (conj b v)]))
      (reduce-kv [[] []] (vec coll))))

(defn- find-pairs-by
  "Base function for find-pairs-by-* functions."
  [xf delimiter-pairs]
  (->> (split-alternate delimiter-pairs)
       (eduction xf (map #(reduce str "" %)))))

(defn find-pairs-by-regex
  "Find delimiter pairs in search-string, returning two sequences of the
  opening and closing delimiters. Uses regex matching."
  [delimiter-pairs search-string]
  (let [xf (map #(re-seq (re-quote-set %) search-string))]
    (find-pairs-by xf delimiter-pairs)))

(defn find-pairs-by-char
  "Find delimiter pairs in search-string, returning two sequences of the
  opening and closing delimiters. Uses filtering by character."
  [delimiter-pairs search-string]
  (let [xf (map #(filter (set %) search-string))]
    (find-pairs-by xf delimiter-pairs)))

(def find-pairs find-pairs-by-char)

(defn -main
  "Run Task 1 with a given input D and S, defaulting to the first example from
  the task description."
  [& args]
  (let [[D S] (or args DEFAULT-INPUT)]
    (cl-format true "~{~a~%~}" (find-pairs D S))))