Skip to content

Commit

Permalink
test if Clerk works
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Oct 16, 2024
1 parent 044af24 commit fb9a659
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
83 changes: 83 additions & 0 deletions clojure/day01_clerk.clj
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down

0 comments on commit fb9a659

Please sign in to comment.