This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn spiral [data] | |
| (let [side (count (first data)) | |
| directions [[0 1] [1 0] [0 -1] [-1 0]] | |
| steps (cons side (mapcat (partial repeat 2) (range (dec side) -1 -1))) | |
| subscripts (mapcat repeat steps (cycle directions))] | |
| (map (partial get-in data) | |
| (->> subscripts | |
| (reductions (partial mapv +) [0 -1]) | |
| (take (inc (* side side))) | |
| rest)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| if [[ -z "$1" ]]; then | |
| echo 'no service name provided' | |
| exit 0 | |
| fi | |
| NAME="$1" | |
| NAME_CAMEL=$(echo "$1" | sed -r 's/(^|-)([a-z])/\U\2/g') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns advent-2020-15) | |
| (def seed [1,2,16,19,18,0]) | |
| (defn next-num [[data [i n]]] | |
| [(assoc data n i) | |
| [(inc i) | |
| (- i (data n i))]]) | |
| (defn nth-num [seed n] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; part 1 | |
| (let [ds (map (fn [[a b]] (- b a)) (partition 2 1 (cons 0 (sort data-10))))] | |
| (* (count (filter #{1} ds)) | |
| (inc (count (filter #{3} ds))))) | |
| ;; part 2 | |
| (defn nexts [tail] | |
| (when-let [[x & xs] (seq tail)] | |
| (take-while #(and (seq %) (<= (- (first %) x) 3)) | |
| (iterate rest xs)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; part 1 | |
| (def target (->> (partition 26 1 data-9) | |
| (some (fn [data] | |
| (let [nums (set (butlast data)) | |
| lst (last data)] | |
| (when (not-any? #(nums (- lst %)) nums) | |
| lst)))))) | |
| ;; part 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (def instructions (->> input-8 | |
| (re-seq #"(\w+) ((?:\+|-)\d+)") | |
| (map (comp rest #(update % 2 clojure.edn/read-string))) | |
| vec)) | |
| (defn path [instructions] (iterate (fn [[n acc :as curr]] | |
| (let [[cmd x] (instructions n)] | |
| (map + curr (case cmd | |
| "nop" [1 0] | |
| "jmp" [x 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn parse-bag-rule [s] | |
| (let [[_ bag contents] (re-matches #"^(.*?) bags contain (.*)$" s) | |
| bags (re-seq #"(\d+) (\w+ \w+)" contents)] | |
| [bag (into {} (map (fn [[_ n tag]] | |
| [tag (clojure.edn/read-string n)]) | |
| bags))])) | |
| (def bags (into {} (map parse-bag-rule (clojure.string/split-lines input-7)))) | |
| ;; part 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (def data-6 (clojure.string/split in-6 #"\n\n")) | |
| ;; part1 | |
| (apply + (map #(count (disj (set %) \newline)) data-6)) | |
| ;; part2 | |
| (apply + (map #(let [g-size (inc (count (filter #{\newline} %)))] | |
| (->> (frequencies %) | |
| vals | |
| (filter #{g-size}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn locator [r n] | |
| (fn [in] | |
| (->> (iterate #(/ % 2) (/ n 2)) | |
| (map vector in) | |
| (keep (fn [[ch n]] (when (#{r} ch) n))) | |
| (apply +)))) | |
| (let [get-row (locator \B 128) | |
| get-col (locator \R 8)] | |
| (defn get-seat [in] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun take (n coll) | |
| "takes at most n items from list, additional values are: | |
| nth tail (the rest of list, with n items dropped) | |
| designator of the sufficient items in coll for requested amount" | |
| (declare (type list coll) | |
| (type fixnum n)) | |
| (the (values list list (member t nil)) | |
| (loop repeat n | |
| for x on coll | |
| collecting (car x) into res |
NewerOlder