Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/24-game-Solve/Clojure/24-game-solve.clj
Normal file
25
Task/24-game-Solve/Clojure/24-game-solve.clj
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(ns rosettacode.24game.solve
|
||||
(:require [clojure.math.combinatorics :as c]
|
||||
[clojure.walk :as w]))
|
||||
|
||||
(def ^:private op-maps
|
||||
(map #(zipmap [:o1 :o2 :o3] %) (c/selections '(* + - /) 3)))
|
||||
|
||||
(def ^:private patterns '(
|
||||
(:o1 (:o2 :n1 :n2) (:o3 :n3 :n4))
|
||||
(:o1 :n1 (:o2 :n2 (:o3 :n3 :n4)))
|
||||
(:o1 (:o2 (:o3 :n1 :n2) :n3) :n4)))
|
||||
|
||||
(defn play24 [& digits]
|
||||
{:pre (and (every? #(not= 0 %) digits)
|
||||
(= (count digits) 4))}
|
||||
(->> (for [:let [digit-maps
|
||||
(->> digits sort c/permutations
|
||||
(map #(zipmap [:n1 :n2 :n3 :n4] %)))]
|
||||
om op-maps, dm digit-maps]
|
||||
(w/prewalk-replace dm
|
||||
(w/prewalk-replace om patterns)))
|
||||
(filter #(= (eval %) 24))
|
||||
(map println)
|
||||
doall
|
||||
count))
|
||||
Loading…
Add table
Add a link
Reference in a new issue