Skip to content

Commit

Permalink
solve queen attack exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes committed Sep 8, 2023
1 parent 826feb3 commit 9b0d561
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function testExercisesUntilFail() {
}

//testSolution(randExercise())
//testSolution("say")
testSolution("queen_attack")
//loadExercise("all_your_base")
//testExercisesUntilFail()
//testExercises()
2 changes: 1 addition & 1 deletion test/exercises.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"rna_transcription" : "(defn dna->rna [x]\r\n (cond \r\n (= x \"G\") \"C\"\r\n (= x \"C\") \"G\" \r\n (= x \"A\") \"U\"\r\n (= x \"T\") \"A\")) \r\n\r\n(dna->rna \"C\") \r\n\r\n (defn- translate [c]\r\n (dna->rna c)) \r\n\r\n(defn to-rna [dna]\r\n (apply str (map translate dna)))\r\n",
"pascals_triangle" : "(defn next-row [row]\r\n (let [padded (concat [0] row [0])\r\n partitioned (partition 2 1 padded)]\r\n (vec (map #(reduce + 0 %) partitioned))))\r\n\r\n(defn row-helper [n accum]\r\n (cond (= n 1) (reverse accum)\r\n :else (row-helper (dec n) (cons (next-row (first accum)) accum))))\r\n\r\n(defn triangle-fn [n]\r\n (row-helper n (list [1])))\r\n\r\n(def triangle (triangle-fn 3))\r\n\r\n(defn row [n]\r\n (last (triangle-fn n)))\r\n",
"space_age" : "(def year-seconds (* 365.25 24 60 60))\r\n\r\n(defmacro defperiod [planet ratio]\r\n `(defn ~(symbol (str \"on-\" planet))\r\n [seconds#]\r\n (/ seconds# (* ~ratio year-seconds))))\r\n\r\n(defperiod \"earth\" 1.0)\r\n(defperiod \"mercury\" 0.2408467)\r\n(defperiod \"venus\" 0.61519726)\r\n(defperiod \"mars\" 1.8808158)\r\n(defperiod \"jupiter\" 11.862615)\r\n(defperiod \"saturn\" 29.447498)\r\n(defperiod \"uranus\" 84.016846)\r\n(defperiod \"neptune\" 164.79132)\r\n",
"queen_attack" : "(defn abs [n]\r\n (if (neg? n) (- n) n))\r\n\r\n(def empty-board\r\n (->> [\"_\" \"_\" \"_\" \"_\" \"_\" \"_\" \"_\" \"_\"]\r\n (repeat 8)\r\n vec))\r\n\r\n(defn board->str [board]\r\n (->> board\r\n (map #(str/join \" \" %))\r\n (map #(str % \"\\n\"))\r\n (apply str)))\r\n\r\n(defn board-string [m]\r\n (-> empty-board\r\n (let [G__21143 (:w m)]\r\n (if (assoc-in (:w m) \\W)\r\n (-> G__21143 (:b m))\r\n G__21143))\r\n board->str))\r\n\r\n(defn can-attack [m]\r\n (let [w (:w m) b (:b m)\r\n wx (first w) wy (last w)\r\n bx (first b) by (last b)]\r\n (or (= wx bx)\r\n (= wy by)\r\n (= (abs (- wx bx))\r\n (abs (- wy by))))))\r\n",
"queen_attack" : "(defn abs [n]\r\n (if (neg? n) (- n) n))\r\n\r\n(defn board->str [board]\r\n (->> board\r\n (map #(str/join \" \" %))\r\n (map #(str % \"\\n\"))\r\n (apply str)))\r\n\r\n(defn cell-string [{:keys [w b]} coords]\r\n (cond\r\n (= coords w) \"W\"\r\n (= coords b) \"B\"\r\n :else \"_\"))\r\n\r\n(defn row-string [queens row]\r\n (->> (range 8)\r\n (map #(cell-string queens [row, %]))\r\n (str/join \" \")))\r\n\r\n(defn board-string [queens]\r\n (->> (range 8)\r\n (map #(row-string queens %))\r\n (map #(str % \"\\n\"))\r\n str/join))\r\n\r\n(defn can-attack [{:keys [w b]}]\r\n (let [diffs (map #(abs (- %1 %2)) w b)]\r\n (or (some zero? diffs)\r\n (apply = diffs))))\r\n",
"acronym" : "(defn acronym [text]\r\n (->> (re-seq #\"[A-Z]+[a-z]*|[a-z]+\" text)\r\n (map ffirst)\r\n (apply str)\r\n str/upper-case))\r\n",
"secret_handshake" : "(defn int->reversed-binary [int]\r\n (-> int\r\n (Integer/toBinaryString)\r\n (reverse)))\r\n\r\n(defn convert [integer]\r\n (remove nil?\r\n (map (fn [command binary]\r\n (when (= \\1 binary) command))\r\n [\"wink\" \"double blink\" \"close your eyes\" \"jump\"]\r\n (int->reversed-binary integer))))\r\n\r\n(defn commands [integer]\r\n (let [cmds (convert integer)]\r\n (if (= \\1 (nth (int->reversed-binary integer) 4 \"0\"))\r\n (reverse cmds)\r\n cmds)))\r\n",
"crypto_square" : "(defn normalize-plaintext [plaintext]\r\n (str/lower-case (str/replace plaintext #\"[^\\w]\" \"\")))\r\n\r\n(defn square-size [plaintext]\r\n (int (Math/ceil (Math/sqrt (count (normalize-plaintext plaintext))))))\r\n\r\n(defn plaintext-segments [plaintext]\r\n (let [normalized-plaintext (normalize-plaintext plaintext)\r\n size (square-size normalized-plaintext)]\r\n (map #(apply str %) (partition size size normalized-plaintext))))\r\n\r\n(defn padded-segments [plaintext pad]\r\n (let [segments (plaintext-segments plaintext)\r\n r (count (first segments))]\r\n (map #(concat % (take (- r (count %)) (repeat pad))) segments)))\r\n\r\n(defn ciphertext [plaintext]\r\n (apply str (apply mapcat vector (padded-segments plaintext nil))))\r\n\r\n(defn normalize-ciphertext [plaintext]\r\n (str/join \" \" (map #(apply str %) (apply map vector (padded-segments plaintext \\ )))))\r\n",
Expand Down

0 comments on commit 9b0d561

Please sign in to comment.