aboutsummaryrefslogtreecommitdiff
path: root/challenge-101
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-02-23 15:40:57 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-02-25 14:11:04 -0800
commit1b4ace0cc2d0c7dce94718b0e61a30bddd575867 (patch)
treebe110db350fa05ed4b90b0ac70971474830f8cd3 /challenge-101
parent2c26164a5a90aa14a19078d845769d3ec9fbb5ae (diff)
downloadperlweeklychallenge-club-1b4ace0cc2d0c7dce94718b0e61a30bddd575867.tar.gz
perlweeklychallenge-club-1b4ace0cc2d0c7dce94718b0e61a30bddd575867.tar.bz2
perlweeklychallenge-club-1b4ace0cc2d0c7dce94718b0e61a30bddd575867.zip
Ch101 (Clojure): Tasks 1 & 2
Diffstat (limited to 'challenge-101')
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/pom.xml24
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/core.clj12
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t1.clj47
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t2.clj28
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/util/matrix.clj20
-rw-r--r--challenge-101/tyler-wardhaugh/clojure/test/tw/weekly/c101_test.clj16
8 files changed, 147 insertions, 13 deletions
diff --git a/challenge-101/tyler-wardhaugh/clojure/README.md b/challenge-101/tyler-wardhaugh/clojure/README.md
index f978c858a6..372957fa28 100644
--- a/challenge-101/tyler-wardhaugh/clojure/README.md
+++ b/challenge-101/tyler-wardhaugh/clojure/README.md
@@ -1,13 +1,13 @@
-# tw.weekly.c100
+# tw.weekly.c101
-The Weekly Challenge - #100 - Tyler Wardhaugh
+The Weekly Challenge - #101 - Tyler Wardhaugh
## Usage
Run the project directly (shows default output from both tasks):
- $ clojure -M -m tw.weekly.c100.core
+ $ clojure -M -m tw.weekly.c101.core
Run the project's tests (which are samples from the task descriptions):
@@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions):
Run Task #1 with input
- $ clojure -M -m tw.weekly.c100.t1 T
+ $ clojure -M -m tw.weekly.c101.t1 A
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c100.t2 T
+ $ clojure -M -m tw.weekly.c101.t2 A B C
## Project Template
diff --git a/challenge-101/tyler-wardhaugh/clojure/deps.edn b/challenge-101/tyler-wardhaugh/clojure/deps.edn
index c245240206..5a7bdda9e2 100644
--- a/challenge-101/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-101/tyler-wardhaugh/clojure/deps.edn
@@ -1,6 +1,7 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
- clojure.java-time {:mvn/version "0.3.2"}}
+ org.clojure/math.numeric-tower {:mvn/version "0.0.4"}
+ 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-101/tyler-wardhaugh/clojure/pom.xml b/challenge-101/tyler-wardhaugh/clojure/pom.xml
index eb6b9afc9c..f3c877ab99 100644
--- a/challenge-101/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-101/tyler-wardhaugh/clojure/pom.xml
@@ -2,11 +2,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tw.weekly</groupId>
- <artifactId>tw.weekly.c100</artifactId>
+ <artifactId>tw.weekly.c101</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c100</name>
- <description>Challenge #100</description>
- <url>https://github.com/tw.weekly/tw.weekly.c100</url>
+ <name>tw.weekly.c101</name>
+ <description>Challenge #101</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c101</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c100</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c100.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c100.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c101</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c101.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c101.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
@@ -30,6 +30,16 @@
<artifactId>clojure</artifactId>
<version>1.10.1</version>
</dependency>
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>math.numeric-tower</artifactId>
+ <version>0.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>net.mikera</groupId>
+ <artifactId>core.matrix</artifactId>
+ <version>0.62.0</version>
+ </dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
diff --git a/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/core.clj b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/core.clj
new file mode 100644
index 0000000000..216602a2a6
--- /dev/null
+++ b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c101.core
+ (:require [tw.weekly.c101.t1 :as t1])
+ (:require [tw.weekly.c101.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1:")
+ (t1/-main)
+ (println "\nTask #2:")
+ (t2/-main))
diff --git a/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t1.clj b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t1.clj
new file mode 100644
index 0000000000..be5ec63b41
--- /dev/null
+++ b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t1.clj
@@ -0,0 +1,47 @@
+(ns tw.weekly.c101.t1
+ (:require [clojure.edn :as edn]
+ [clojure.core.matrix :as m]
+ [clojure.math.numeric-tower :as math]
+ [tw.weekly.util.matrix :as um]))
+
+;;;
+; Task description for TASK #1 › Pack a Spiral
+;;;
+
+(def DEFAULT-INPUT [1 2 3 4])
+
+(defn get-min-size
+ "Determine the minimum shape necessary for a matrix to hold len items."
+ [len]
+ (let [source (range 1 (inc (quot len 2)))
+ xf (comp
+ (filter #(zero? (mod len %1)))
+ (map (juxt #(math/abs (- % (quot len %))) identity)))
+ f (partial min-key first)]
+ (->> source
+ (transduce xf (completing f) [len len])
+ second
+ ((juxt identity (partial quot len))))))
+
+(defn- pack
+ "Recursively pack an incoming coll into an m x n spiral matrix. This is a
+ helper function for pack-spiral, not meant to be called directly."
+ [coll m n]
+ (condp = 1
+ m (vector coll)
+ n (map vector (reverse coll))
+ (m/join (um/rotate-matrix (pack (drop n coll) n (dec m)))
+ (vector (take n coll)))))
+
+(defn pack-spiral
+ "Return coll as a packed spiral matrix."
+ [coll]
+ (let [[m n] (get-min-size (count coll))]
+ (m/matrix (pack coll m n))))
+
+(defn -main
+ "Run Task 1 using a list A, defaulting to the example given in the task
+ description."
+ [& args]
+ (let [A (or (some-> args first edn/read-string) DEFAULT-INPUT)]
+ (um/print-matrix (pack-spiral A))))
diff --git a/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t2.clj b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t2.clj
new file mode 100644
index 0000000000..a74ca720f4
--- /dev/null
+++ b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/c101/t2.clj
@@ -0,0 +1,28 @@
+(ns tw.weekly.c101.t2
+ (:require [clojure.edn :as edn]))
+
+;;;
+; Task description for TASK #2 › Origin-containing Triangle
+;;;
+
+(def DEFAULT-INPUT [[0 1] [1 0] [2 2]])
+
+(defn contains-origin
+ "Determine if the triangle formed by the given three points cover the (0,0)
+ origin on a 2D plane, using the Barycentric Coordinate Sytem method from
+ https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html"
+ [[x1 y1] [x2 y2] [x3 y3]]
+ (let [denominator (+ (* (- y2 y3) (- x1 x3)) (* (- x3 x2) (- y1 y3)))
+ A (/ (+ (* (- y2 y3) (- x3)) (* (- x3 x2) (- y3))) denominator)
+ B (/ (+ (* (- y3 y1) (- x3)) (* (- x1 x3) (- y3))) denominator)
+ C (- 1 A B)]
+ (and (<= 0 A) (<= A 1)
+ (<= 0 B) (<= B 1)
+ (<= 0 C) (<= C 1))))
+
+(defn -main
+ "Run Task 2 points A, B, and C; defaulting to the example given in the
+ task description."
+ [& args]
+ (let [[A B C] (or (some->> args (take 3) (map edn/read-string)) DEFAULT-INPUT)]
+ (println (if (contains-origin A B C) 1 0))))
diff --git a/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/util/matrix.clj b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/util/matrix.clj
new file mode 100644
index 0000000000..1754c558c5
--- /dev/null
+++ b/challenge-101/tyler-wardhaugh/clojure/src/tw/weekly/util/matrix.clj
@@ -0,0 +1,20 @@
+(ns tw.weekly.util.matrix
+ (:require [clojure.pprint :as pp]
+ [clojure.string :as str]
+ [clojure.core.matrix :as m]))
+
+(defn rotate-matrix
+ "Rotate a matrix counterclockwise"
+ [mat]
+ (-> mat m/transpose reverse))
+
+(defn print-matrix
+ "Pretty print a matrix"
+ [mat]
+ (let [ks (range (m/dimension-count mat 1))
+ tbl (with-out-str
+ (pp/print-table ks (map (partial zipmap ks) mat)))]
+ (-> tbl
+ (str/split #"\n")
+ (->> (drop 3)
+ (run! println)))))
diff --git a/challenge-101/tyler-wardhaugh/clojure/test/tw/weekly/c101_test.clj b/challenge-101/tyler-wardhaugh/clojure/test/tw/weekly/c101_test.clj
new file mode 100644
index 0000000000..8535bdf99d
--- /dev/null
+++ b/challenge-101/tyler-wardhaugh/clojure/test/tw/weekly/c101_test.clj
@@ -0,0 +1,16 @@
+(ns tw.weekly.c101-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c101.t1 :refer [pack-spiral]]
+ [tw.weekly.c101.t2 :refer [contains-origin]]))
+
+(deftest task-1
+ (testing "Task 1, Pack a Spiral"
+ (is (= [[4 3] [1 2]] (pack-spiral [1 2 3 4])))
+ (is (= [[5 4] [6 3] [1 2]] (pack-spiral (range 1 7))))
+ (is (= [[8 7 6] [9 12 5] [10 11 4] [1 2 3]] (pack-spiral (range 1 13))))))
+
+(deftest task-2
+ (testing "Task 2, Origin-containing Triangle"
+ (is (false? (apply contains-origin [[0 1] [1 0] [2 2]])))
+ (is (true? (apply contains-origin [[1 1] [-1 1] [0 -3]])))
+ (is (true? (apply contains-origin [[0 1] [2 0] [-6 0]])))))