Guest User

Untitled

a guest
Sep 3rd, 2013
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns oops.core
  2.   (:require
  3.    [clojure.math.combinatorics :as combi]
  4.    [instaparse.core :as insta]))
  5.  
  6. (def deck
  7.   (concat
  8.    [\c]            ;; wild cantor
  9.    (repeat 4 \p)   ;; summoner's pact
  10.    (repeat 4 \l)   ;; lotus petal
  11.    (repeat 4 \x)   ;; mox
  12.    (repeat 4 \d)   ;; dark ritual
  13.    (repeat 4 \r)   ;; cabal ritual
  14.    (repeat 8 \g)   ;; spirit guide
  15.    (repeat 4 \m)   ;; manamorphose
  16.    (repeat 8 \w)   ;; voittokeino
  17.    (repeat 4 \t)   ;; tappo
  18.    (repeat 4 \n)   ;; narcomoeba
  19.    (repeat 3 \h))) ;; cabal therapy
  20.  
  21. (def bnf
  22.   "S           = 1B 2MORE win ANY* |
  23.                 2B (1MORE | dark) win ANY*
  24.  
  25.   NOWINCON    = 1B 2MORE NOTWIN* |
  26.                 2B 1MORE NOTWIN*
  27.  
  28.   ONEMANASHORT= 1B 1MORE win (NOTMANA* | mox*) |
  29.                 2B win (NOTMANA* | mox*)
  30.  
  31.   2MORE       = 1MORE 1MORE | dark
  32.  
  33.   2B          = petal dark |
  34.                 BLACKMOX dark |
  35.                 CANTORBLACK dark |
  36.                 1B 1MORE
  37.  
  38.   1B          = manamorph (pact | guide | mox (manamorph | cantor)) COLORLESS |
  39.                 petal COLORLESS |
  40.                 BLACKMOX COLORLESS |
  41.                 CANTORBLACK COLORLESS
  42.  
  43.   CANTORBLACK = (pact | guide | (mox manamorph)) (pact | cantor)
  44.   1MORE       = pact | guide | petal | cabal | BLACKMOX | NONBLACKMOX
  45.   COLORLESS   = pact | guide | petal | BLACKMOX | NONBLACKMOX
  46.   NONBLACKMOX = mox (manamorph | cantor | narco)
  47.   BLACKMOX    = mox (dark | cabal | therapy | win)
  48.   ANY         = NOTWIN | win
  49.   NOTWIN      = cantor | pact | petal | mox | dark | cabal | guide | manamorph | kill | narco | therapy
  50.   NOTMANA     = cantor | manamorph | kill | narco | therapy | win
  51.   cantor      = 'c'
  52.   petal       = 'l'
  53.   pact        = 'p'
  54.   guide       = 'g'
  55.   mox         = 'x'
  56.   dark        = 'd'
  57.   cabal       = 'r'
  58.   manamorph   = 'm'
  59.   win         = 'w'
  60.   kill        = 't'
  61.   narco       = 'n'
  62.   therapy     = 'h'")
  63.  
  64.  
  65.  
  66. (def parser
  67.   (insta/parser bnf))
  68.  
  69. (defn hands
  70.   [n]
  71.   (->>
  72.    (combi/combinations deck n)
  73.    (map sort)
  74.    frequencies
  75.    seq))
  76.  
  77. (defn kill?
  78.   ([hand] (kill? hand :S))
  79.   ([hand start-symbol]
  80.     (loop [hands (combi/permutations hand)]
  81.       (if (empty? hands)
  82.         false
  83.         (let [cur (apply str (first hands))
  84.               rst (rest hands)]
  85.           (if-not (insta/failure? (parser cur :start start-symbol))
  86.             (parser cur :start start-symbol)
  87.             (recur rst)))))))
  88.  
  89. (defn filter-with-all
  90.   [coll & preds]
  91.   (reduce
  92.      (fn [c pred]
  93.        (filter pred c))
  94.      coll
  95.      preds))
  96.  
  97. (defn filter-hands
  98.   [hands & preds]
  99.   (let [hand-preds (map #(fn [[hand _]] (% hand)) preds)]
  100.     (apply filter-with-all hands hand-preds)))
  101.  
  102. (defn count-hands
  103.   [hands]
  104.   (->>
  105.    hands
  106.    (map (fn [[_ amount]] amount))
  107.    (reduce +)))
  108.  
  109. (defn first-turn-kills
  110.   [hands]
  111.   (filter-hands
  112.    hands
  113.    #(some #{\w} %)
  114.    kill?))
  115.  
  116. (defn one-mana-short
  117.   [hands]
  118.   (filter-hands
  119.    hands
  120.    #(some #{\w} %)
  121.    #(kill? % :ONEMANASHORT)))
  122.  
  123. (defn no-win-con
  124.   [hands]
  125.   (filter-hands
  126.    hands
  127.    #(not-any? #{\w} %)
  128.    #(kill? % :NOWINCON)))
  129.  
  130. (println (count-hands (no-win-con (hands 7)))) ;; => 4200393
  131. (println (count-hands (one-mana-short (hands 7)))) ;; => 4630860
  132. (println (count-hands (first-turn-kills (hands 7)))) ;; => 3895364
Advertisement
Add Comment
Please, Sign In to add comment