From fb9a659890e35b4c37b4e5bdf49a1573c6be1237 Mon Sep 17 00:00:00 2001 From: narimiran Date: Wed, 16 Oct 2024 11:42:09 +0200 Subject: [PATCH] test if Clerk works --- .github/workflows/test.yml | 26 ++++++++++++ clojure/day01_clerk.clj | 83 ++++++++++++++++++++++++++++++++++++++ deps.edn | 11 ++++- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 clojure/day01_clerk.clj diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1717c9f..491847a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,3 +49,29 @@ jobs: - name: Benchmark solutions if : ${{ matrix.kind == 'benchmark' }} run: clojure -M:profile clojure/tests/solutions_benchmark.clj + + - name: Clerk Cache + uses: actions/cache@v2 + with: + path: .cache + key: ${{ runner.os }}-clerk + + - name: Clerk Build + run: clojure -X:nextjournal/clerk + + - name: Upload artifact + uses: actions/upload-pages-artifact@v1 + with: + # Upload entire repository + path: 'public/build' + + deploy: + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + runs-on: ubuntu-latest + needs: build + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v1 diff --git a/clojure/day01_clerk.clj b/clojure/day01_clerk.clj new file mode 100644 index 0000000..be89da5 --- /dev/null +++ b/clojure/day01_clerk.clj @@ -0,0 +1,83 @@ +;; # Day 1: Trebuchet?! +;; + + + + +^{:nextjournal.clerk/visibility {:code :hide :result :hide}} +(ns day01-clerk + {:nextjournal.clerk/auto-expand-results? true} + (:require aoc + [clojure.string :as str] + [nextjournal.clerk :as clerk])) + +;; ## Loading the data + +;; Let's load both the example input and the real one: +(def example-input (aoc/read-file "01_test")) +(def input (aoc/read-file 1)) + +;; Parsing the input is easy, just split the lines of the input: +(def example-data (aoc/parse-input example-input)) +(def data (aoc/parse-input input)) + + +;; ## Helpers +;; +;; For Part 2, the numbers can be spelled out with letters. +;; We'll convert them back to digits with the following mappping: +(def words + {"one" "1" + "two" "2" + "three" "3" + "four" "4" + "five" "5" + "six" "6" + "seven" "7" + "eight" "8" + "nine" "9"}) + + +;; Now, the regex pattern to match either numbers as words or plain numbers: +(def word-pattern (str/join "|" (keys words))) +(def first-patt (re-pattern (str word-pattern #"|\d"))) + +;; For Part 2, we need to have these word patterns backwards: +(def last-patt (re-pattern (str (str/reverse word-pattern) #"|\d"))) + + + +;; ## Calculate calibration value + +;; To calculate the calibration value, we need to find the first and +;; the last number in a line and concat them togeter: +(defn calibration-value [line patt rev-patt] + (let [first-match (re-find patt line) + last-match (str/reverse (re-find rev-patt (str/reverse line))) + first-digit (words first-match first-match) + last-digit (words last-match last-match)] + (parse-long (str first-digit last-digit)))) + +;; For example: +(def data-sample (take 10 data)) +(clerk/table {"line" data-sample + "p1 score" (mapv #(calibration-value % #"\d" #"\d") data-sample) + "p2 score" (mapv #(calibration-value % first-patt last-patt) data-sample)}) + + + + + +;; We need to take a sum of a calibration value of each line: +(defn calibration-sum [lines patt rev-patt] + (aoc/sum-map #(calibration-value % patt rev-patt) lines)) + + + +;; ## Putting it all together: +(defn solve [input] + (let [lines (aoc/parse-input input)] + [(calibration-sum lines #"\d" #"\d") + (calibration-sum lines first-patt last-patt)])) + +(solve input) diff --git a/deps.edn b/deps.edn index e801322..99eb928 100644 --- a/deps.edn +++ b/deps.edn @@ -4,12 +4,21 @@ org.clojure/data.int-map {:mvn/version "1.3.0"} org.clojure/data.priority-map {:mvn/version "1.2.0"} org.flatland/ordered {:mvn/version "1.15.12"} - io.github.engelberg/better-cond {:git/tag "v2.1.5" :git/sha "156bc5f"}} + io.github.engelberg/better-cond {:git/tag "v2.1.5" :git/sha "156bc5f"} + io.github.nextjournal/clerk {:git/sha "a6dc483ffea2a8ecf5c2725dea6df72efbc39eee"}} :aliases {:profile {:jvm-opts ["-Djdk.attach.allowAttachSelf"] :extra-deps {criterium/criterium {:mvn/version "0.4.6"} com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.3.0"} com.taoensso/tufte {:mvn/version "2.6.3"}}} + :nextjournal/clerk {:exec-fn nextjournal.clerk/build! + :exec-args {:paths ["clojure/day01_clerk.clj" + "clojure/day02.clj"] + #_#_:compile-css true + #_#_:package :single-file} + #_#_:main-opts ["-m" "babashka.cli.exec"] + #_#_:extra-deps {org.slf4j/slf4j-nop {:mvn/version "2.0.16"} + org.babashka/cli {:mvn/version "0.8.60"}}} :viz {:extra-deps {quil/quil {:mvn/version "4.3.1563"}}} :splint {:extra-deps {io.github.noahtheduke/splint {:mvn/version "1.17.0"}} :main-opts ["-m" "noahtheduke.splint"]}