aboutsummaryrefslogtreecommitdiff
path: root/challenge-089
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-06 10:42:46 +0000
committerGitHub <noreply@github.com>2020-12-06 10:42:46 +0000
commita922b1a04252ff0329fb17c433592ffa32fbcf2d (patch)
tree837eff94c5da05e8794c941c9f812c429900c564 /challenge-089
parentcf5bc91717e6a06f1d410a6f2aea7e4fddde24cc (diff)
parent948067565ec965727e469008ea211895c5c25760 (diff)
downloadperlweeklychallenge-club-a922b1a04252ff0329fb17c433592ffa32fbcf2d.tar.gz
perlweeklychallenge-club-a922b1a04252ff0329fb17c433592ffa32fbcf2d.tar.bz2
perlweeklychallenge-club-a922b1a04252ff0329fb17c433592ffa32fbcf2d.zip
Merge pull request #2910 from tylerw/tw/challenge-089
Challenge 089 in Clojure
Diffstat (limited to 'challenge-089')
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/README.md12
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/deps.edn6
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/pom.xml19
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/core.clj12
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t1.clj21
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t2.clj38
-rw-r--r--challenge-089/tyler-wardhaugh/clojure/test/tw/weekly/c89_test.clj28
-rw-r--r--challenge-089/tyler-wardhaugh/lua/README.md4
-rwxr-xr-xchallenge-089/tyler-wardhaugh/lua/run.lua9
9 files changed, 132 insertions, 17 deletions
diff --git a/challenge-089/tyler-wardhaugh/clojure/README.md b/challenge-089/tyler-wardhaugh/clojure/README.md
index 49181152f7..f5a89af18d 100644
--- a/challenge-089/tyler-wardhaugh/clojure/README.md
+++ b/challenge-089/tyler-wardhaugh/clojure/README.md
@@ -1,13 +1,13 @@
-# tw.weekly.c88
+# tw.weekly.c89
-The Weekly Challenge - #088 - Tyler Wardhaugh
+The Weekly Challenge - #089 - Tyler Wardhaugh
## Usage
Run the project directly (shows default output from both tasks):
- $ clojure -M -m tw.weekly.c88.core
+ $ clojure -M -m tw.weekly.c89.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.c88.t1 N1 N2 N3...
+ $ clojure -M -m tw.weekly.c89.t1 N
-Run Task #2 with input:
+Run Task #2:
- $ clojure -M -m tw.weekly.c88.t2 MATRIX-FILE
+ $ clojure -M -m tw.weekly.c89.t2
## Project Template
diff --git a/challenge-089/tyler-wardhaugh/clojure/deps.edn b/challenge-089/tyler-wardhaugh/clojure/deps.edn
index f09671a417..cfd5d782d1 100644
--- a/challenge-089/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-089/tyler-wardhaugh/clojure/deps.edn
@@ -1,7 +1,9 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
- net.mikera/core.matrix {:mvn/version "0.62.0"}
- org.clojure/math.numeric-tower {:mvn/version "0.0.4"}}
+ org.clojure/core.logic {:mvn/version "1.0.0"}
+ org.clojure/math.combinatorics {:mvn/version "0.1.6"}
+ org.clojure/math.numeric-tower {:mvn/version "0.0.4"}
+ org.clojure/tools.macro {:mvn/version "0.1.2"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
diff --git a/challenge-089/tyler-wardhaugh/clojure/pom.xml b/challenge-089/tyler-wardhaugh/clojure/pom.xml
index 6038b97734..3097f51f8e 100644
--- a/challenge-089/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-089/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.c88</artifactId>
+ <artifactId>tw.weekly.c89</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c88</name>
- <description>Challenge #088</description>
- <url>https://github.com/tw.weekly/tw.weekly.c88</url>
+ <name>tw.weekly.c89</name>
+ <description>Challenge #089</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c89</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c88</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c88.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c88.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c89</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c89.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c89.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
@@ -35,6 +35,11 @@
<artifactId>core.matrix</artifactId>
<version>0.62.0</version>
</dependency>
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>math.numeric-tower</artifactId>
+ <version>0.0.4</version>
+ </dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
diff --git a/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/core.clj b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/core.clj
new file mode 100644
index 0000000000..f3bd12eeb8
--- /dev/null
+++ b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c89.core
+ (:require [tw.weekly.c89.t1 :as t1])
+ (:require [tw.weekly.c89.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1:")
+ (t1/-main)
+ (println "\nTask #2:")
+ (t2/-main))
diff --git a/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t1.clj b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t1.clj
new file mode 100644
index 0000000000..f8bd9c96ae
--- /dev/null
+++ b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t1.clj
@@ -0,0 +1,21 @@
+(ns tw.weekly.c89.t1
+ (:require [clojure.edn :as edn])
+ (:require [clojure.math.combinatorics :as combo])
+ (:require [clojure.math.numeric-tower :as math]))
+
+;;;
+; Task description for TASK #1 › GCD Sum
+;;;
+
+(defn gcd-sum
+ "Sum of the GCD of all possible unique pairs between 1 and n."
+ [n]
+ (let [xf (map (partial apply math/gcd))]
+ (transduce xf + (-> (range 1 (inc n)) (combo/combinations 2)))))
+
+(defn -main
+ "Run Task 1 with a list of numbers N, defaulting to the
+ first example given in the task description."
+ [& args]
+ (let [N (or (some-> args first edn/read-string) 3)]
+ (println (gcd-sum N))))
diff --git a/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t2.clj b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t2.clj
new file mode 100644
index 0000000000..8db6f9fb5a
--- /dev/null
+++ b/challenge-089/tyler-wardhaugh/clojure/src/tw/weekly/c89/t2.clj
@@ -0,0 +1,38 @@
+(ns tw.weekly.c89.t2
+ (:require [clojure.tools.macro :refer [symbol-macrolet]])
+ (:require [clojure.core.logic :as l])
+ (:require [clojure.core.logic.fd :as fd]))
+
+;;;
+; Task description for TASK #2 › Magical Matrix
+;;;
+
+(defn magic-matrix
+ "Compute the magic matrix for a 3x3 square."
+ []
+ (symbol-macrolet
+ [_ (l/lvar)]
+ (let [a _ b _ c _
+ d _ e _ f _
+ g _ h _ i _
+ m [[a b c] [d e f] [g h i]]
+ cm (apply concat m)]
+ (first
+ (l/run 1 [q]
+ (l/== q m)
+ (l/everyg (fn [q] (fd/in q (fd/interval 1 9))) cm)
+ (fd/distinct cm)
+ (fd/eq
+ (= 15 (+ a b c))
+ (= 15 (+ d e f))
+ (= 15 (+ g h i))
+ (= 15 (+ a d g))
+ (= 15 (+ b e h))
+ (= 15 (+ c f i))
+ (= 15 (+ a e i))
+ (= 15 (+ c e g))))))))
+
+(defn -main
+ "Run Task 2"
+ []
+ (run! println (magic-matrix)))
diff --git a/challenge-089/tyler-wardhaugh/clojure/test/tw/weekly/c89_test.clj b/challenge-089/tyler-wardhaugh/clojure/test/tw/weekly/c89_test.clj
new file mode 100644
index 0000000000..db9b34598f
--- /dev/null
+++ b/challenge-089/tyler-wardhaugh/clojure/test/tw/weekly/c89_test.clj
@@ -0,0 +1,28 @@
+(ns tw.weekly.c89-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c89.t1 :refer [gcd-sum]]
+ [tw.weekly.c89.t2 :refer [magic-matrix]]))
+
+(deftest task-1
+ (testing "Task 1, GCD Sum"
+ (is (= 3 (gcd-sum 3)))))
+
+(defn transpose [m]
+ (apply mapv vector m))
+
+(defn rotate [m]
+ (reverse (transpose m)))
+
+(def task-2-possible-answers
+ (let [base [[2 7 6]
+ [9 5 1]
+ [4 3 8]]
+ reversed (vec (reverse base))
+ transposed (transpose base)
+ rev-xposed (vec (reverse transposed))
+ unrotated [base reversed transposed rev-xposed]]
+ (into (set unrotated) (map (comp vec rotate)) unrotated)))
+
+(deftest task-2
+ (testing "Task 2, Magical Matrix"
+ (is (some? (task-2-possible-answers (magic-matrix))))))
diff --git a/challenge-089/tyler-wardhaugh/lua/README.md b/challenge-089/tyler-wardhaugh/lua/README.md
index 9473c1a0a4..d2f91b15fe 100644
--- a/challenge-089/tyler-wardhaugh/lua/README.md
+++ b/challenge-089/tyler-wardhaugh/lua/README.md
@@ -1,13 +1,13 @@
# The Weekly Challenge
-The Weekly Challenge - #088 - Tyler Wardhaugh
+The Weekly Challenge - #089 - Tyler Wardhaugh
## Usage
Run Task 1:
- $ ./run.lua ch-1 N1 N2 N3...
+ $ ./run.lua ch-1 N
Run Task 2:
diff --git a/challenge-089/tyler-wardhaugh/lua/run.lua b/challenge-089/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-089/tyler-wardhaugh/lua/run.lua
@@ -0,0 +1,9 @@
+#!/usr/bin/env lua
+
+local filename = arg[1]
+local run_args = table.move(arg, 2, #arg, 1, {})
+
+io.write(string.format("Running task from '%s' with {%s}:\n",
+ filename, table.concat(run_args, ", ")))
+
+require(filename).run(run_args)