Skip to content

Commit fb9a659

Browse files
committed
test if Clerk works
1 parent 044af24 commit fb9a659

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

.github/workflows/test.yml

+26
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,29 @@ jobs:
4949
- name: Benchmark solutions
5050
if : ${{ matrix.kind == 'benchmark' }}
5151
run: clojure -M:profile clojure/tests/solutions_benchmark.clj
52+
53+
- name: Clerk Cache
54+
uses: actions/cache@v2
55+
with:
56+
path: .cache
57+
key: ${{ runner.os }}-clerk
58+
59+
- name: Clerk Build
60+
run: clojure -X:nextjournal/clerk
61+
62+
- name: Upload artifact
63+
uses: actions/upload-pages-artifact@v1
64+
with:
65+
# Upload entire repository
66+
path: 'public/build'
67+
68+
deploy:
69+
environment:
70+
name: github-pages
71+
url: ${{ steps.deployment.outputs.page_url }}
72+
runs-on: ubuntu-latest
73+
needs: build
74+
steps:
75+
- name: Deploy to GitHub Pages
76+
id: deployment
77+
uses: actions/deploy-pages@v1

clojure/day01_clerk.clj

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;; # Day 1: Trebuchet?!
2+
;;
3+
4+
5+
6+
7+
^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
8+
(ns day01-clerk
9+
{:nextjournal.clerk/auto-expand-results? true}
10+
(:require aoc
11+
[clojure.string :as str]
12+
[nextjournal.clerk :as clerk]))
13+
14+
;; ## Loading the data
15+
16+
;; Let's load both the example input and the real one:
17+
(def example-input (aoc/read-file "01_test"))
18+
(def input (aoc/read-file 1))
19+
20+
;; Parsing the input is easy, just split the lines of the input:
21+
(def example-data (aoc/parse-input example-input))
22+
(def data (aoc/parse-input input))
23+
24+
25+
;; ## Helpers
26+
;;
27+
;; For Part 2, the numbers can be spelled out with letters.
28+
;; We'll convert them back to digits with the following mappping:
29+
(def words
30+
{"one" "1"
31+
"two" "2"
32+
"three" "3"
33+
"four" "4"
34+
"five" "5"
35+
"six" "6"
36+
"seven" "7"
37+
"eight" "8"
38+
"nine" "9"})
39+
40+
41+
;; Now, the regex pattern to match either numbers as words or plain numbers:
42+
(def word-pattern (str/join "|" (keys words)))
43+
(def first-patt (re-pattern (str word-pattern #"|\d")))
44+
45+
;; For Part 2, we need to have these word patterns backwards:
46+
(def last-patt (re-pattern (str (str/reverse word-pattern) #"|\d")))
47+
48+
49+
50+
;; ## Calculate calibration value
51+
52+
;; To calculate the calibration value, we need to find the first and
53+
;; the last number in a line and concat them togeter:
54+
(defn calibration-value [line patt rev-patt]
55+
(let [first-match (re-find patt line)
56+
last-match (str/reverse (re-find rev-patt (str/reverse line)))
57+
first-digit (words first-match first-match)
58+
last-digit (words last-match last-match)]
59+
(parse-long (str first-digit last-digit))))
60+
61+
;; For example:
62+
(def data-sample (take 10 data))
63+
(clerk/table {"line" data-sample
64+
"p1 score" (mapv #(calibration-value % #"\d" #"\d") data-sample)
65+
"p2 score" (mapv #(calibration-value % first-patt last-patt) data-sample)})
66+
67+
68+
69+
70+
71+
;; We need to take a sum of a calibration value of each line:
72+
(defn calibration-sum [lines patt rev-patt]
73+
(aoc/sum-map #(calibration-value % patt rev-patt) lines))
74+
75+
76+
77+
;; ## Putting it all together:
78+
(defn solve [input]
79+
(let [lines (aoc/parse-input input)]
80+
[(calibration-sum lines #"\d" #"\d")
81+
(calibration-sum lines first-patt last-patt)]))
82+
83+
(solve input)

deps.edn

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
org.clojure/data.int-map {:mvn/version "1.3.0"}
55
org.clojure/data.priority-map {:mvn/version "1.2.0"}
66
org.flatland/ordered {:mvn/version "1.15.12"}
7-
io.github.engelberg/better-cond {:git/tag "v2.1.5" :git/sha "156bc5f"}}
7+
io.github.engelberg/better-cond {:git/tag "v2.1.5" :git/sha "156bc5f"}
8+
io.github.nextjournal/clerk {:git/sha "a6dc483ffea2a8ecf5c2725dea6df72efbc39eee"}}
89
:aliases
910
{:profile {:jvm-opts ["-Djdk.attach.allowAttachSelf"]
1011
:extra-deps {criterium/criterium {:mvn/version "0.4.6"}
1112
com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.3.0"}
1213
com.taoensso/tufte {:mvn/version "2.6.3"}}}
14+
:nextjournal/clerk {:exec-fn nextjournal.clerk/build!
15+
:exec-args {:paths ["clojure/day01_clerk.clj"
16+
"clojure/day02.clj"]
17+
#_#_:compile-css true
18+
#_#_:package :single-file}
19+
#_#_:main-opts ["-m" "babashka.cli.exec"]
20+
#_#_:extra-deps {org.slf4j/slf4j-nop {:mvn/version "2.0.16"}
21+
org.babashka/cli {:mvn/version "0.8.60"}}}
1322
:viz {:extra-deps {quil/quil {:mvn/version "4.3.1563"}}}
1423
:splint {:extra-deps {io.github.noahtheduke/splint {:mvn/version "1.17.0"}}
1524
:main-opts ["-m" "noahtheduke.splint"]}

0 commit comments

Comments
 (0)