From 61693aedc19be0752aca365702514f4b325b921f Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Fri, 4 Sep 2020 13:43:16 -0700 Subject: Task #2 --- challenge-076/tyler-wardhaugh/clojure/deps.edn | 3 +- challenge-076/tyler-wardhaugh/clojure/pom.xml | 5 ++ .../tyler-wardhaugh/clojure/resources/dict.txt | 53 +++++++++++++++++++ .../tyler-wardhaugh/clojure/resources/grid.txt | 19 +++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj | 1 + .../tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj | 59 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c76_test.clj | 8 ++- 7 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 challenge-076/tyler-wardhaugh/clojure/resources/dict.txt create mode 100644 challenge-076/tyler-wardhaugh/clojure/resources/grid.txt create mode 120000 challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj create mode 100644 challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj diff --git a/challenge-076/tyler-wardhaugh/clojure/deps.edn b/challenge-076/tyler-wardhaugh/clojure/deps.edn index 2befca222b..fff9b934dc 100644 --- a/challenge-076/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-076/tyler-wardhaugh/clojure/deps.edn @@ -1,7 +1,8 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} com.hypirion/primes {:mvn/version "0.2.2"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"}} + org.clojure/math.combinatorics {:mvn/version "0.1.6"} + net.mikera/core.matrix {:mvn/version "0.62.0"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} diff --git a/challenge-076/tyler-wardhaugh/clojure/pom.xml b/challenge-076/tyler-wardhaugh/clojure/pom.xml index 418d05029f..5f42d66326 100644 --- a/challenge-076/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-076/tyler-wardhaugh/clojure/pom.xml @@ -34,6 +34,11 @@ math.combinatorics 0.1.6 + + net.mikera + core.matrix + 0.62.0 + diff --git a/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt b/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt new file mode 100644 index 0000000000..c0d8215b4a --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt @@ -0,0 +1,53 @@ +argos +margo +patna +traci +tracie +aimed +align +antes +arose +ashed +blunt +blunts +broad +buries +clove +cloven +constitution +constitutions +croon +depart +departed +enter +filch +garlic +goats +grieve +grieves +hazard +liens +malign +malignant +malls +midst +ought +ovary +parted +pudgiest +quash +quashed +ruses +shrine +shrines +social +socializing +spasm +spasmodic +succor +succors +theorem +theorems +virus +viruses +wigged diff --git a/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt b/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt new file mode 100644 index 0000000000..31cf2e0fd8 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt @@ -0,0 +1,19 @@ +B I D E M I A T S U C C O R S T +L D E G G I W Q H O D E E H D P +U S E I R U B U T E A S L A G U +N G N I Z I L A I C O S C N U D +T G M I D S T S A R A R E I F G +S R E N M D C H A S I V E E L I +S C S H A E U E B R O A D M T E +H W O V L P E D D L A I U L S S +R Y O N L A S F C S T A O G O T +I G U S S R R U G O V A R Y O C +N R G P A T N A N G I L A M O O +E I H A C E I V I R U S E S E D +S E T S U D T T G A R L I C N H +H V R M X L W I U M S N S O T B +A E A O F I L C H T O D C A E U +Z S C D F E C A A I I R L N R F +A R I I A N Y U T O O O U T P F +R S E C I S N A B O S C N E R A +D R S M P C U U N E L T E S I L diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj new file mode 120000 index 0000000000..5a32e17ef9 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj @@ -0,0 +1 @@ +ch_2.clj \ No newline at end of file diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj new file mode 100644 index 0000000000..cd5109aab1 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj @@ -0,0 +1,59 @@ +(ns tw.weekly.ch-2 + (:require [clojure.edn :as edn]) + (:require [clojure.java.io :as io]) + (:require [clojure.string :as str]) + (:require [clojure.core.matrix :as mat])) + +(def min-word-length 5) + +(defn parse-grid-file + "Parse grid file and return a multidimensional vector of chars." + [grid-file] + (with-open [rdr (io/reader grid-file)] + (-> rdr + line-seq + seq + (->> (map #(remove (fn [c] (Character/isWhitespace c)) %))) + to-array-2d))) + +(defn parse-dict-file + "Parse dictionary and return a vector of words." + [dict-file] + (with-open [rdr (io/reader dict-file)] + (->> (line-seq rdr) + (filter #(>= (count %) min-word-length)) + (map str/trim-newline) + (map str/upper-case) + (into [])))) + +(defn build-lines + "Return a set of lines in a word grid that could contain valid words." + [grid] + (let [[m n] (mat/shape grid) + transposed (mat/transpose grid) + rotated (mapv #(vec (reverse (mat/get-row transposed %))) (range n)) + get-diags (fn [g d] (map #(mat/diagonal g %) (range (- d) d))) + diag-across (get-diags grid m) + diag-down (get-diags rotated n)] + (->> [grid transposed diag-across diag-down] + (mapcat #(into [] (comp (map str/join) (remove (partial = ""))) %)) + (mapcat (juxt identity str/reverse)) + (into #{})))) + +(defn word-search + "Return a sequence of words found in a grid from a dictionary." + [grid dict] + (let [all-lines (build-lines grid)] + (->> dict + (mapcat (fn [word] (keep #(when (str/includes? % word) word) all-lines)))))) + +(defn -main + "Run Task 2 with a grid file and a dictionary file, defaulting to the ones in the resources directory." + [& args] + (let [grid-file (or (some-> args first edn/read-string) (io/resource "grid.txt")) + dict-file (or (some-> args second edn/read-string) (io/resource "dict.txt")) + grid (parse-grid-file grid-file) + dict (parse-dict-file dict-file) + words (word-search grid dict)] + (printf "%d word(s) from the dictionary of %d word(s) found in the grid:\n" (count words) (count dict)) + (doseq [word words] (println word)))) diff --git a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj index de205dd328..003ce81591 100644 --- a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj +++ b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj @@ -1,7 +1,8 @@ (ns tw.weekly.c76-test (:require [clojure.test :refer :all] + [clojure.java.io :as io] [tw.weekly.ch-1 :refer [find-min-primes-to-sum find-min-primes-to-sum-brute-force]] - [tw.weekly.ch-2 :refer []])) + [tw.weekly.ch-2 :refer [parse-dict-file parse-grid-file word-search]])) (deftest ch-1 (testing "Task 1" @@ -10,4 +11,7 @@ (deftest ch-2 (testing "Task 2" - )) + (let [grid (parse-grid-file (io/resource "grid.txt")) + dict (parse-dict-file (io/resource "dict.txt")) + words (word-search grid dict)] + (is (= (into #{} dict) (into #{} words)))))) -- cgit