Skip to content

Commit

Permalink
Merge branch 'map-varargs' of https://github.com/bobbicodes/bien
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes committed Sep 8, 2023
2 parents 5f10b9b + b80c401 commit 4c9a1d3
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 29 deletions.
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ function testExercisesUntilFail() {
console.log("Fails:", fails)
}

//testSolution(randExercise())
//testSolution("go_counting")
testSolution(randExercise())
//testSolution("powerset")
//loadExercise("go_counting")
//testExercisesUntilFail()
//testExercises()
36 changes: 25 additions & 11 deletions scratch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,23 @@
" W W "
" W "])

(apply mapv vector grid)

(defn grid->board [grid]
(->> (apply mapv vector grid)
(mapv #(mapv {\space nil \B :black \W :white} %))))
(mapv #(mapv {" " nil "B" :black "W" :white} %))))

(def board (grid->board grid))
(def point [0 1])
(def points #{point})

(count (grid->board grid))
(loop [points #{point}]
(let [new-points (->> (mapcat (partial neighbors board) points)
(filter #(nil? (get-in board %)))
set
(set/union points))]
(cond
(get-in board point) #{}
(= points new-points) points
:else (recur new-points))))

(defn invalid? [board [x y]]
(or (neg? x)
Expand All @@ -360,14 +370,18 @@
(def point [0 1])
(def points #{point})

;; loop local
(def points
#{[0 1] [0 0]})
(def new-points
(->> (mapcat (partial neighbors board) points)
(filter #(nil? (get-in board %)))
set
(set/union points)))


(->> (mapcat (partial neighbors board) points)
(filter #(nil? (get-in board %)))
set
(set/union points))
(set/union
(set
(filter #(nil? (get-in board %))
(mapcat (partial neighbors board) points)))
points)

(loop [points #{point}]
(let [new-points (->> (mapcat (partial neighbors board) points)
Expand Down
Empty file added scratch.json
Empty file.
2 changes: 2 additions & 0 deletions src/clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@
(defn get-in [m ks]
(reduce #(get % %2) m ks))

(defn some? [x] (not (nil? x)))

(defn update
([m k f]
(assoc m k (f (get m k))))
Expand Down
1 change: 0 additions & 1 deletion src/clj/pprint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@
(defn pprint [obj]
(if (nil? obj) "nil"
(do (console-print obj)
(console-print (nil? obj))
(cond (empty? obj) (str obj)
:else (pp- obj 0)))))
36 changes: 27 additions & 9 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,33 +354,51 @@ function js_method_call(object_method_str) {
return js_to_mal(res);
}

function toSet() {
return new Set(arguments[0])
function toSet(coll) {
var new_set = new Set()
for (const item of seq(coll)) {
if (!contains_Q(new_set, item)) {
new_set.add(item)
}
}
return new_set
}

function _union(setA, setB) {
const _union = new Set(setA);
for (const elem of setB) {
_union.add(elem);
if (!contains_Q(_union, elem)) {
_union.add(elem);
}
}
return _union;
}

function _intersection(setA, setB) {
const _intersection = new Set();
for (const elem of setB) {
if (setA.has(elem)) {
if (contains_Q(setA, (elem))) {
_intersection.add(elem);
}
}
return _intersection;
}

function setDelete(set, item) {
var new_set = new Set()
for (const i of set) {
if (!types._equal_Q(i, item)) {
new_set.add(i)
}
}
return new_set
}

function symmetricDifference(setA, setB) {
const _difference = new Set(setA);
var _difference = new Set(setA);
for (const elem of setB) {
if (_difference.has(elem)) {
_difference.delete(elem);
if (contains_Q(_difference, elem)) {
_difference = setDelete(_difference, elem);
} else {
_difference.add(elem);
}
Expand All @@ -389,9 +407,9 @@ function symmetricDifference(setA, setB) {
}

function _difference(setA, setB) {
const _difference = new Set(setA);
var _difference = new Set(setA);
for (const elem of setB) {
_difference.delete(elem);
_difference = setDelete(_difference, elem);
}
return _difference;
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,6 @@ evalString("(do " + core_clj + ")")
evalString("(do " + pprint + ")")

export const repp = function (str) {
return evalString("(do " + str + ")")
//return EVAL(READ("(pprint " + "(do " + str + "))"), repl_env)
//return evalString("(do " + str + ")")
return EVAL(READ("(pprint " + "(do " + str + "))"), repl_env)
};
6 changes: 3 additions & 3 deletions test/exercises.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
"mycomp" : "(defn mycomp\r\n ([] identity)\r\n ([f] f)\r\n ([f g]\r\n (fn\r\n ([] (f (g)))\r\n ([x] (f (g x)))\r\n ([x y] (f (g x y)))\r\n ([x y z] (f (g x y z)))\r\n ([x y z & args] (f (apply g x y z args)))))\r\n ([f g & fs]\r\n (reduce comp (list* f g fs))))",
"k" : "(defn k [i s]\r\n (set\r\n (if (= i 0)\r\n [#{}]\r\n (mapcat #(for [p (k (- i 1) %2)] (conj p %))\r\n s (next (iterate next s))))))",
"myreductions" : "(defn my-reductions [f init xs]\r\n (loop [s xs acc init res [init]]\r\n (if (empty? s)\r\n res\r\n (recur (rest s)\r\n (f acc (first s))\r\n (conj res (f acc (first s)))))))",
"conj_set" : "(def n 0)",
"conj_set" : "(def n 2)",
"double" : "(def f \r\n (fn [n]\r\n (* n 2)))",
"sym_diff" : "(defn sym-diff [a b]\r\n (set/symmetric-difference a b))",
"tri_path" : "(defn tri-path [s]\r\n (first\r\n (reduce\r\n #(map + (map min (butlast %1) (rest %1)) %2)\r\n (reverse s))))",
"db" : "(defn digits [n b]\r\n (if (< n b)\r\n [n]\r\n (conj (digits (quot n b) b) (rem n b))))",
"sets" : "(def s #{:a :b :c})",
"sets" : "(def s #{:a :b :c :d})",
"mypal" : "(defn mypal [s]\r\n (= (seq s) (reverse (seq s))))",
"cartesian" : "(defn cartesian [x y]\r\n (set (for [a x b y] [a b])))",
"perfect_square" : "(defn perfect-square [s]\r\n (let [l (re-seq #\"\\\\d+\" s)]\r\n (str/join \",\" (filter #{\"4\" \"9\" \"16\" \"25\" \"36\"} l))))",
Expand Down Expand Up @@ -161,7 +161,7 @@
"difference_of_squares" : "(defn sum [xs] (reduce + 0 xs))\r\n\r\n(defn sum-of-squares [n]\r\n (sum (map #(int (Math/pow % 2)) (range 0 (inc n)))))\r\n\r\n(defn square-of-sum [n]\r\n (int (Math/pow (sum (range 0 (inc n))) 2)))\r\n\r\n(defn difference [x]\r\n (- (square-of-sum x) (sum-of-squares x)))\r\n",
"complex_numbers" : "(defn real [c] (first c))\r\n\r\n(defn imaginary [c] (last c))\r\n\r\n(defn abs [c] (Math/sqrt (apply + (map #(Math/pow % 2) [(first c) (last c)]))))\r\n\r\n(defn conjugate [c] [(first c) (- (last c))])\r\n\r\n(defn add [c1 c2] [(+ (first c1) (first c2)) (+ (last c1) (last c2))])\r\n\r\n(defn sub [c1 c2] [(- (first c1) (first c2)) (- (last c1) (last c2))])\r\n\r\n(defn mul [c1 c2] [(- (* (first c1) (first c2)) (* (last c1) (last c2)))\r\n (+ (* (first c1) (last c2)) (* (last c1) (first c2)))])\r\n\r\n(defn div [c1 c2]\r\n (when-not (== 0 c1 c2)\r\n (let [x (+ (* (first c2) (first c2)) (* (last c2) (last c2)))]\r\n [(double (/ (+ (* (first c1) (first c2)) (* (last c1) (last c2))) x))\r\n (double (/ (- (* (last c1) (first c2)) (* (first c1) (last c2))) x))])))\r\n",
"poker" : "(def base-values\r\n {\"2\" 2\r\n \"3\" 3\r\n \"4\" 4\r\n \"5\" 5\r\n \"6\" 6\r\n \"7\" 7\r\n \"8\" 8\r\n \"9\" 9\r\n \"10\" 10\r\n \"J\" 11\r\n \"Q\" 12\r\n \"K\" 13})\r\n\r\n(def values-low-as\r\n (into base-values\r\n {\"A\" 1}))\r\n\r\n(def values-high-as\r\n (into base-values\r\n {\"A\" 14}))\r\n\r\n#_(defn value-freq [hand]\r\n (->> hand\r\n (group-by :value)\r\n (vals)\r\n (map count)\r\n (sort)))\r\n\r\n#_(defn one-pair? [hand]\r\n (= [1 1 1 2] (value-freq hand)))\r\n\r\n#_(defn two-pair? [hand]\r\n (= [1 2 2] (value-freq hand)))\r\n\r\n#_(defn three-of-a-kind? [hand]\r\n (= [1 1 3] (value-freq hand)))\r\n\r\n#_(defn straight? [hand]\r\n (->> hand\r\n (map :value)\r\n (sort)\r\n (partition 2 1)\r\n (map (partial apply -))\r\n (every? (partial = -1))))\r\n\r\n#_(defn flush? [hand]\r\n (->> hand\r\n (map :color)\r\n (apply =)))\r\n\r\n#_(defn full-house? [hand]\r\n (= [2 3] (value-freq hand)))\r\n\r\n#_(defn four-of-a-kind? [hand]\r\n (= [1 4] (value-freq hand)))\r\n\r\n#_(defn straight-flush? [hand]\r\n (and (flush? hand) (straight? hand)))\r\n\r\n#_(defn category [hand]\r\n (condp #(%1 %2) hand\r\n straight-flush? 8\r\n four-of-a-kind? 7\r\n full-house? 6\r\n flush? 5\r\n straight? 4\r\n three-of-a-kind? 3\r\n two-pair? 2\r\n one-pair? 1\r\n 0))\r\n\r\n#_(defn highcards [hand]\r\n (->> hand\r\n (map :value)\r\n (frequencies)\r\n (map reverse)\r\n (map vec)\r\n (sort)\r\n (reverse)\r\n (vec)))\r\n\r\n#_(defn read-hand [raw-hand values]\r\n (vec (for [[_ rank color] (re-seq #\"(\\d+|[JQKA])([CDHS])\" raw-hand)]\r\n {:value (values rank)\r\n :color color})))\r\n\r\n#_(defn score\r\n ([raw-hand]\r\n (last (sorted-set (score raw-hand values-high-as)\r\n (score raw-hand values-low-as))))\r\n ([raw-hand values]\r\n (-> raw-hand\r\n (read-hand values)\r\n ((juxt category highcards)))))\r\n\r\n#_(defn max-by-score [raw-hands]\r\n (->> raw-hands\r\n (group-by score)\r\n (into (sorted-map))\r\n (vals)\r\n (last)))\r\n\r\n#_(defn best-hands [hands]\r\n (max-by-score hands))\r\n",
"go_counting" : "(defn grid->board [grid]\r\n (->> (mapv #(vec (mapcat vector %)) grid)\r\n (mapv #(mapv { \" \" nil \"B\" :black \"W\" :white} %))))\r\n\r\n(defn invalid? [board [x y]]\r\n (or (neg? x)\r\n (neg? y)\r\n (>= x (count board))\r\n (>= y (count (first board)))))\r\n\r\n(defn neighbors [board [x y]]\r\n (->> [[x (dec y)] [x (inc y)] [(inc x) y] [(dec x) y]]\r\n (remove (partial invalid? board))))\r\n\r\n#_(defn point->territory [board point]\r\n (loop [points #{point}]\r\n (let [new-points (->> (mapcat (partial neighbors board) points)\r\n (filter #(nil? (get-in board %)))\r\n set\r\n (set/union points))]\r\n (cond\r\n (get-in board point) #{}\r\n (= points new-points) points\r\n :else (recur new-points)))))\r\n\r\n(defn territory [grid [x y]]\r\n (let [board (grid->board grid)\r\n t (point->territory board [x y])]\r\n {:stones t\r\n :owner (->> t\r\n (mapcat (partial neighbors board))\r\n (map (partial get-in board))\r\n (remove nil?)\r\n set\r\n (get {#{:black} :black #{:white} :white}))}))\r\n\r\n(defn territories [grid]\r\n (let [ts (->> (for [y (range (count grid))\r\n x (range (count (first grid)))]\r\n [x y])\r\n (mapv (partial territory grid))\r\n (map (fn [m] {(:owner m) (:stones m)}))\r\n (apply (partial merge-with set/union)))]\r\n {:black-territory (get ts :black #{})\r\n :white-territory (get ts :white #{})\r\n :null-territory (get ts nil)}))\r\n",
"go_counting" : "(defn get-point [grid [x y]]\r\n (get-in grid [y x]))\r\n\r\n(defn adj [grid [x y]]\r\n (->> [[(inc x) y] [(dec x) y] [x (inc y)] [x (dec y)]]\r\n (filter #(get-point grid %))))\r\n\r\n(defn update-owner [old new]\r\n (when (#{new :unknown} old) new))\r\n\r\n(defn territory [grid point]\r\n (loop [[p & more] [point]\r\n {:keys [stones] :as res} {:stones #{} :owner :unknown}]\r\n (cond\r\n (nil? p) (update res :owner #(when (seq stones) (#{:white :black} %)))\r\n (stones p) (recur more res)\r\n :else (if-let [owner (-> (get-point grid p) {\\W :white \\B :black})]\r\n (recur more (update res :owner update-owner owner))\r\n (recur (concat more (adj grid p)) (update res :stones conj p))))))\r\n\r\n(defn all-points [grid]\r\n (for [x (range (count (first grid)))\r\n y (range (count grid))]\r\n [x y]))\r\n\r\n(def owner-key-mapping\r\n {:black :black-territory :white :white-territory nil :null-territory})\r\n\r\n(defn territories [grid]\r\n (loop [[p & more] (all-points grid)\r\n res {:black-territory #{} :white-territory #{} :null-territory #{}}]\r\n (cond\r\n (nil? p) res\r\n (some #(% p) (vals res)) (recur more res)\r\n :else (let [{:keys [stones owner]} (territory grid p)\r\n k (owner-key-mapping owner)]\r\n (recur more (update res k into stones))))))\r\n",
"accumulate" : "(defn accumulate [f xs]\r\n (loop [xs xs\r\n accum []]\r\n (if (empty? xs)\r\n accum\r\n (recur (rest xs) (conj accum (f (first xs)))))))\r\n",
"hexadecimal" : "(defn- char-between [start end c] (let [ascii (int c)]\r\n (and (>= ascii (int start)) (<= ascii (int end)))))\r\n\r\n(def is-digit (partial char-between \\0 \\9))\r\n(def is-a-to-f (partial char-between \\a \\f))\r\n\r\n(defn- is-hex-digit [c]\r\n (or (is-digit c) (is-a-to-f c)))\r\n\r\n(defn- digit-to-int [c]\r\n (cond\r\n (is-digit c) (- (int c) (int \\0))\r\n (is-a-to-f c) (+ (- (int c) (int \\a)) 10)\r\n :else (throw \"Character is not a hex digit\")))\r\n\r\n(defn hex-to-int [digits]\r\n (if\r\n (some (complement is-hex-digit) digits) 0\r\n (reduce (fn [a b] (+ (digit-to-int b) (* a 16))) 0 digits)))\r\n",
"series" : "(defn slices [string n]\r\n (if (zero? n)\r\n [\"\"]\r\n (loop [string string, acc []]\r\n (if (< (count string) n)\r\n acc\r\n (recur (rest string) (conj acc (apply str (take n string))))))))\r\n",
Expand Down
Loading

0 comments on commit 4c9a1d3

Please sign in to comment.