Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuenzalida committed Jul 22, 2019
0 parents commit b1718ca
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.cpcache
/out
/.nrepl-port
/cljs-test-runner-out
*~
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Euler CLJC

Cross platform Project Euler solutions.

This project is based on from the [Advent of CLJC](https://github.com/borkdude/advent-of-cljc) project.

## Contribute

PRs welcome. Make a new solution file with the `new` script:

script/new 012 username

where `username` is your Github or Bitbucket username. Then fill in the solution in the file. If the input and answers are still empty you will have to provide it in `data.cljc`.

This repo does not accept multiple inputs and answers.

## Dev

Read [here](https://nrepl.xyz/nrepl/usage/server.html) how to get an nREPL for this project.

## Tests

Make sure the tests for your solution pass with the `test-one` script.

Please do not run calculations outside the tests. Memoized functions are permitted. Top-level lazy sequences are fine as long as they are not realized outside the tests.

Tests support the following metadata:

- `:skip-cljs`: used for skipping Node tests. Used in `.circle/test-diff`,
`script/test` and `script/test-one`.
- `:slow`: used for skipping long running tests. Only used in `script/test`.

Run all tests:

script/test

Run one test:

script/test-one 012 username

Run with instrumentation:

INSTRUMENT=true script/test
INSTRUMENT=true script/test-one euler.012.username

Skip Clojure or ClojureScript:

SKIP_CLJ=true script/test
SKIP_CLJS=true script/test

33 changes: 33 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{:deps {net.cgrand/xforms {:mvn/version "0.18.2"}
com.rpl/specter {:mvn/version "1.1.2"}
binaryage/oops {:mvn/version "0.6.4"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/clojure {:mvn/version "1.10.0-RC5"}
;; org.clojure/clojurescript {:mvn/version "1.10.439"}
org.clojure/clojurescript {:git/url "https://github.com/clojure/clojurescript"
:sha "d6f8896452b531a273f99f2716aaa08f09600063"}
org.clojure/test.check {:mvn/version "RELEASE"}
;;org.clojure/spec.alpha {:local/root "/Users/Borkdude/git/spec.alpha"}
chivorcam {:mvn/version "1.0.0"}
speculative {:mvn/version "0.0.3-SNAPSHOT"}
;; workarounds-1.10.439 {:git/url "https://github.com/mfikes/workarounds-1.10.439"
;; :sha "a559201602fc6a21701479b83086d76bd5ed09eb"}
}}
:test-clj
{:extra-paths ["test"]
:extra-deps {com.cognitect/test-runner
{:git/url "https://github.com/cognitect-labs/test-runner.git"
:sha "028a6d41ac9ac5d5c405dfc38e4da6b4cc1255d5"}}
:jvm-opts ["-Xss2m"]
:main-opts ["-m" "cognitect.test-runner"]}
:test-cljs
{:extra-paths ["test" "cljs-test-runner-out/gen"]
:extra-deps {olical/cljs-test-runner {;;:mvn/version "3.2.1"
:git/url "https://github.com/borkdude/cljs-test-runner"
:sha "c04e52ba1f9b2c646710b532a0c3ca5cab429491"}}
:main-opts ["-m" "cljs-test-runner.main"]}
:new
{:extra-paths ["test"]
:extra-deps {org.clojure/tools.cli {:mvn/version "0.4.1"}}
:main-opts ["-m" "aoc.new"]}}}
3 changes: 3 additions & 0 deletions script/new
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

clj -A:new $@
20 changes: 20 additions & 0 deletions script/rescore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -eu

if [ ! $YEAR ]; then
YEAR=2018
fi

branch="rescore-$YEAR-$1"
git checkout -b "$branch"
cd "src/aoc/y$YEAR/$1"

for i in $(ls *.cljc); do
echo -e ";; trigger scoring, this comment may be removed next time\n" >> $i
done
git commit -am "$branch"
git push origin "$branch"

git checkout master
git branch -D "$branch"
44 changes: 44 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

exit_code=0

if [ ! $INSTRUMENT ]; then
regex="euler\.p.*(?<!data)"
exclude_flag="-e"
exclude_pattern="instrumented"
else
regex="problem\.instrument|problem\.p.*(?<!data)"
fi

if [ ! $SKIP_CLJ ]; then
echo "=== Running clojure tests $exclude"
clojure -J-Xmx3200m \
-R:test:test-clj \
-O:test-clj \
-e "(require,'patch.clj-2443)" \
-m cognitect.test-runner -d src -r "$regex" \
-e "slow" \
"$exclude_flag" "$exclude_pattern"
fi

if [ ! $? ]; then
exit_code=1
fi

if [ ! $SKIP_CLJS ]; then
echo -e "\n=== Running cljs tests"
mkdir -p cljs-test-runner-out/gen
clojure -A:test:test-cljs \
-d src -r "$regex" \
-e "slow" \
-e "skip-cljs" \
"$exclude_flag" "$exclude_pattern" \
-c cljs-opts.edn \
-D doo-opts.edn
fi

if [ $? != 0 ]; then
exit_code=1
fi

exit $exit_code
52 changes: 52 additions & 0 deletions script/test-one
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

exit_code=0
ns_flag="-n"
ns="euler.instrument"

if [ $1 ] && [ $2 ]; then
printf -v user_ns "euler.p$1" "$2"
else
user_ns=$1
fi

if [ ! $INSTRUMENT ]; then
exclude_flag="-e"
exclude_pattern="instrumented"
ns_flag="-n"
ns=""
fi

if [ ! $SKIP_CLJ ]; then
echo "=== Running clojure test $1 $2 $3"
clojure -R:test:test-clj \
-O:test-clj \
-e "(require,'patch.clj-2443)" \
-m cognitect.test-runner -d src \
"$ns_flag" "$ns" \
-n "$user_ns" \
"$exclude_flag" "$exclude_pattern"
fi

if [ $? != 0 ]; then
exit_code=1
fi

if [ ! $SKIP_CLJS ]; then
echo -e "\n=== Running cljs test $1 $2 $3"
mkdir -p cljs-test-runner-out/gen
clojure -A:test:test-cljs \
-d src \
"$ns_flag" "$ns" \
-n "$user_ns" \
-e "skip-cljs" \
"$exclude_flag" "$exclude_pattern" \
-c cljs-opts.edn \
-D doo-opts.edn
fi

if [ $? != 0 ]; then
exit_code=1
fi

exit $exit_code
94 changes: 94 additions & 0 deletions test/euler/new.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(ns euler.new
(:require
[clojure.java.io :as io]
[clojure.tools.cli :refer [parse-opts]]))

(defn data-ns [problem]
(format "(ns euler.p%s.data)
(def input)
(def answer-1)
(def answer-2)
" problem))

(defn user-ns [problem user]
(format "(ns euler.p%s.%s
(:refer-clojure :exclude [read-string format])
(:require
[euler.utils :as u :refer [deftest read-string format]]
[euler.p%s.data :refer [input answer-1 answer-2]]
[clojure.test :as t :refer [is testing]]))
(defn solve-1 []
;; TODO
)
(defn solve-2 []
;; TODO
)
(deftest part-1
(is (= (str answer-1)
(str (solve-1)))))
(deftest part-2
(is (= (str answer-2)
(str (solve-2)))))
;;;; Scratch
(comment
(t/run-tests)
)
" problem user problem))

(defn create-new [{:keys [problem user]}]
(when (contains? (into #{} [problem user]) nil)
(throw (Exception. (str "Some required arguments are null: "
{:problem problem
:user user}))))
(let [data-out (io/file "src" "euler"
(str "p" problem)
"data.cljc")
out (io/file "src" "euler"
(str "p" problem)
(str user ".cljc"))]
(io/make-parents out)
(when-not (.exists data-out)
(spit data-out (data-ns problem))
(println "Created a new file at" (.getPath data-out)))
(if-not (.exists out)
(do (spit out (user-ns problem user))
(println "Created a new file at" (.getPath out)))
(println "File already exists:" (.getPath out)))))

(def cli-options
;; An option with a required argument
[["-p" "--problem NUMBER" "Problem"
:parse-fn #(Integer/parseInt %)
:validate [#(<= 1 % 2018) "Must be a number between 1 and 1000 (inclusive)"]]
["-u" "--user USER" "User"
:validate [#(re-find #"^[A-Za-z]" %) "Username must start with letter"]]
["-h" "--help"]])

(defn -main
[& args]
(let [{:keys [options arguments] :as opts}
(parse-opts args cli-options)
{:keys [options summary errors]}
(if (empty? options)
(parse-opts (interleave
["-p" "-u"]
arguments)
cli-options)
opts)]
(cond (:help options)
(println summary)
errors
(doseq [e errors]
(println e))
:else
(create-new options))))

0 comments on commit b1718ca

Please sign in to comment.