This file contains 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 aoc2018-clj.day13 | |
(:require [clojure.string :as str] | |
[clojure.java.io :as io] | |
[clojure.math.combinatorics :as combs] | |
[clojure.set :as set])) | |
(def track (str/split-lines (-> "day13.txt" io/resource slurp))) | |
(def race-track (mapv vec track)) | |
(defn intersection-rule [{:keys [loc dir turn-count] :as car}] |
This file contains 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 NIL (symbol "NIL")) | |
(deftype ConsCell [CAR CDR] | |
clojure.lang.ISeq | |
(cons [this x] (ConsCell. x this)) | |
(first [this] (.CAR this)) | |
;; next and more must return ISeq: | |
;; https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/ISeq.java | |
(more [this] (if | |
(= (.CDR this) NIL) |
This file contains 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
;; couldn't resist after hearing the podcast... | |
(defn ibid [coll] | |
(reduce (fn [res cur] | |
(if (= cur "ibid") | |
(conj res (last res)) | |
(conj res cur))) | |
[(first coll)] | |
(rest coll))) |
This file contains 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
(comment | |
Multimethods are an open dispatch mechanism, but is closed in it's | |
dispatch function. If the set of dispatch types is open, so is | |
the multimethod. | |
Protocols are always open because you can always add a new type. | |
Multimethods require you to pick something to dispatch on that | |
remains open so any user can create a new one. | |
For dependency injection, protocols work great because you can |
This file contains 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
;;;; machinery for multimethods | |
(defmacro defmethod2 [fname dispatch-val signature body] | |
`(swap! ~(symbol (str "multimethod-lkp-" fname)) assoc ~dispatch-val | |
(fn ~signature ~body))) | |
(defn make-generic-fn [fname dispatchfn] | |
`(defn ~fname [& ~'args] | |
(let [dispatch-val# (apply ~(symbol (str "multimethod-dispatch-" fname)) ~'args) | |
mm-table# (deref ~(symbol (str "multimethod-lkp-" fname))) | |
matching-fn# (some #(get mm-table# %) [dispatch-val# :default])] |
This file contains 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
;;; | |
;;; implementation | |
;;; | |
(defn build-fn [[mname args body]] | |
{(keyword mname) (list 'fn mname args body)}) | |
(defn make-generic-fn [[mname args body]] | |
`(defn ~mname ~args | |
(let [f# (get ~(first args) ~(keyword mname))] | |
(f# ~@args)))) |
This file contains 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 protos (atom nil)) | |
(defn build-func [[name args body]] | |
(list 'fn name args body)) | |
(defmacro bleh [fbody] | |
`(let [f# ~(build-func fbody)] | |
(do (reset! protos f#) f#))) | |
(macroexpand-1 '(bleh (foo [y] (+ x y)))) |
This file contains 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
(defmulti destruct-type | |
(fn [binding value] | |
(cond (sequential? binding) clojure.lang.Sequential ; order matters, vector is associative too | |
(associative? binding) clojure.lang.Associative | |
:else (type binding)))) | |
(defn coerce-binding-type [binding-type] | |
(case binding-type | |
:strs str | |
:syms (fn [sym] `'~sym) |
This file contains 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 game {:player1 [26 8 2 17 19 29 41 7 25 33 50 16 36 37 32 4 46 12 21 48 11 6 13 23 9], | |
:player2 [27 47 15 45 10 14 3 44 31 39 42 5 49 24 22 20 30 1 35 38 18 43 28 40 34]}) | |
;; Treating the seen rule as needing to see the exact game (both decks) twice. | |
;; Doesn't terminate. | |
(defn play-round [game] | |
(loop [seen #{} | |
{:keys [player1 player2] :as game} game] | |
(if (or (empty? player1) (empty? player2)) | |
game | |
(let [p1 (player1 0) |
This file contains 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 tetris.game) | |
(def rows 20) | |
(def cols 10) | |
(def board (vec (repeat rows (vec (repeat cols " "))))) | |
(defn new-game-state [] | |
{:board board | |
:active-piece nil | |
:game-over? false |
OlderNewer