Skip to content

Instantly share code, notes, and snippets.

--- Day 1: Trebuchet?! ---
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
As they're making the final ad
(ns areqs.core
(:require [org.httpkit.client :as client])
(:gen-class))
(defn generate-wikis
"returns a list of year wiki pages
like https://en.wikipedia.org/wiki/AD_1"
[n]
(let [base-url "https://en.wikipedia.org/wiki/AD_%s"]
(map (fn [n] (format base-url n)) (range 1 (inc n)))))
✗ rustup target list
aarch64-apple-darwin
aarch64-apple-ios
aarch64-fuchsia
aarch64-linux-android
aarch64-pc-windows-msvc
aarch64-unknown-linux-gnu
aarch64-unknown-linux-musl
aarch64-unknown-none
aarch64-unknown-none-softfloat
(def ranges [
[0, 20000],
[20000, 50000],
[50000, 100000],
[100000, nil],
])
(def percentages [0 5 20 50])
➜ lein deps
Retrieving lurodrigo/firestore-clj/1.2.1/firestore-clj-1.2.1.pom from clojars
Retrieving org/clojure/core.match/1.0.0/core.match-1.0.0.pom from central
Retrieving com/google/firebase/firebase-admin/6.12.2/firebase-admin-6.12.2.pom from central
Retrieving com/google/api-client/google-api-client/1.30.1/google-api-client-1.30.1.pom from central
Retrieving com/google/api-client/google-api-client-parent/1.30.1/google-api-client-parent-1.30.1.pom from central
Retrieving com/google/http-client/google-http-client-bom/1.30.1/google-http-client-bom-1.30.1.pom from central
Retrieving com/google/oauth-client/google-oauth-client-bom/1.30.1/google-oauth-client-bom-1.30.1.pom from central
Retrieving com/google/oauth-client/google-oauth-client/1.30.1/google-oauth-client-1.30.1.pom from central
Retrieving com/google/oauth-client/google-oauth-client-parent/1.30.1/google-oauth-client-parent-1.30.1.pom from central
(defn handle-error [url status error]
(println (format "Error getting %s" url))
(println (format "Status returned: %d" status))
(println (format "Error: %s" error)))
(defn process-response [url headers body]
(println (format "Getting URL %s" url))
(println (format "Last modified: %s" (:last-modified headers)))
(println (format "Body is: %d bytes long" (count body)))
url)
(ns spyder.core
(:require [clojure.core.async :refer [>! <!! go chan]]
[org.httpkit.client :as http])
(:gen-class))
(defn make-str [index request]
(str "#" index ": " (:content-length (:headers request))))
(defn async-get [index url channel]
(http/get url #(go (>! channel (make-str index %)))))
(def formula "(-b ± (sqrt(b * b - (4 * a * c)))) / 2 * a")
;; thread-last version
(->>
formula
(re-seq #"[()]")
(map #(if (= "(" %) 1 -1))
(reductions +)
(apply max))

One liners

When programming under a functional pattern, where there are no variables and there's no much room for state handling the use of one-liners or composing is mandatory.

Lets see an example:

Find the sum of all the multiples of 3 or 5 below 1000.

Iterative example
(ns clojure-noob.core
(:gen-class))
(defn powers [n] (for [x (range)] (reduce * (repeat x n))))
(defn solve
[n x y]
(letfn [(f [m] (range m n m))]
(- (reduce + (concat (f x) (f y))) (reduce + (f (* x y))))))