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 mul [num-str m] | |
| (let [[acc v] (reduce (fn [[acc v] ch] | |
| (let [n (+ v (* m (Integer/parseInt (str ch))))] | |
| [(conj acc (rem n 10)) | |
| (quot n 10)])) | |
| [nil 0] | |
| (reverse num-str))] | |
| (apply str (if (zero? v) acc (conj acc v))))) | |
| (mul "9876545679" 7) |
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
| import java.io.IOException; | |
| import java.net.URI; | |
| import java.nio.file.Paths; | |
| import java.net.http.*; | |
| import java.nio.file.*; | |
| class App { | |
| public static void main(String[] args) throws IOException, InterruptedException { | |
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
| (defpackage :paip2 | |
| (:use :common-lisp | |
| :util) | |
| (:export :sentence | |
| :compose | |
| :mappend | |
| :*simple-grammar* | |
| :*grammar* | |
| :generate | |
| :generate-tree)) |
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
| (defmacro if-let ((name val) t-branch nil-branch) | |
| `(let ((,name ,val)) | |
| (if ,name ,t-branch ,nil-branch))) | |
| (defstruct h-tree | |
| content | |
| weight | |
| left | |
| right) |
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 rand-test () | |
| (= 1 (gcd (random most-positive-fixnum) | |
| (random most-positive-fixnum)))) | |
| (defun estimate-pi (attempts) | |
| (loop repeat attempts | |
| counting (rand-test) into success | |
| finally (return (sqrt (/ 6 (/ success attempts)))))) | |
| (estimate-pi 1000000) |
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 num->bits (n) | |
| (declare (type integer n)) | |
| (the bit-vector | |
| (if (zerop n) | |
| #*0 | |
| (loop | |
| with len fixnum = (1+ (floor (log n 2))) | |
| with res bit-vector = (make-array len :element-type 'bit) | |
| for i fixnum below len | |
| for curr integer = n then (ash curr -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
| (defun fib-iter (a b p q n) | |
| (cond ((zerop n) b) | |
| ((evenp n) (fib-iter a | |
| b | |
| (+ (* p p) (* q q)) | |
| (+ (* 2 p q) (* q q)) | |
| (/ n 2))) | |
| (t (fib-iter (+ (* b q) (* a q) (* a p)) | |
| (+ (* b p) (* a q)) | |
| p |
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 |
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
| (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}) |
OlderNewer