Skip to content

Instantly share code, notes, and snippets.

@allumbra
Last active May 22, 2018 16:27
Show Gist options
  • Save allumbra/f1c1f9089c23d1f032eded80b54f1cfc to your computer and use it in GitHub Desktop.
Save allumbra/f1c1f9089c23d1f032eded80b54f1cfc to your computer and use it in GitHub Desktop.
Coin Game
;; gorilla-repl.fileformat = 1
;; **
;;; # Coin Board Game or Die
;;; From the puzzle found here: http://fivethirtyeight.com/features/can-you-survive-this-deadly-board-game/
;;;
;; **
;; @@
; namespace and includes...
(ns coin-game
(:require [gorilla-plot.core :as plot]))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ## Get a sequence of die rolls that add up to the board length
;;; Demonstrate recursion
;; **
;; @@
; gives a sorted set of spaces touched by a pawn driven by a 6 sided die.
(defn rnd-seq-sum [total]
(loop [sm 0
result []]
(let [r (inc(rand-int 6))
new-sum (+ r sm)
res (conj result new-sum)
]
(if (>= new-sum total)
(apply sorted-set res)
(recur new-sum res)))))
(rnd-seq-sum 100)
;; @@
;; =>
;;; {"type":"list-like","open":"<span class='clj-set'>#{</span>","close":"<span class='clj-set'>}</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>3</span>","value":"3"},{"type":"html","content":"<span class='clj-long'>9</span>","value":"9"},{"type":"html","content":"<span class='clj-long'>12</span>","value":"12"},{"type":"html","content":"<span class='clj-long'>15</span>","value":"15"},{"type":"html","content":"<span class='clj-long'>20</span>","value":"20"},{"type":"html","content":"<span class='clj-long'>25</span>","value":"25"},{"type":"html","content":"<span class='clj-long'>27</span>","value":"27"},{"type":"html","content":"<span class='clj-long'>32</span>","value":"32"},{"type":"html","content":"<span class='clj-long'>34</span>","value":"34"},{"type":"html","content":"<span class='clj-long'>37</span>","value":"37"},{"type":"html","content":"<span class='clj-long'>38</span>","value":"38"},{"type":"html","content":"<span class='clj-long'>40</span>","value":"40"},{"type":"html","content":"<span class='clj-long'>43</span>","value":"43"},{"type":"html","content":"<span class='clj-long'>47</span>","value":"47"},{"type":"html","content":"<span class='clj-long'>50</span>","value":"50"},{"type":"html","content":"<span class='clj-long'>54</span>","value":"54"},{"type":"html","content":"<span class='clj-long'>55</span>","value":"55"},{"type":"html","content":"<span class='clj-long'>59</span>","value":"59"},{"type":"html","content":"<span class='clj-long'>61</span>","value":"61"},{"type":"html","content":"<span class='clj-long'>66</span>","value":"66"},{"type":"html","content":"<span class='clj-long'>70</span>","value":"70"},{"type":"html","content":"<span class='clj-long'>74</span>","value":"74"},{"type":"html","content":"<span class='clj-long'>80</span>","value":"80"},{"type":"html","content":"<span class='clj-long'>82</span>","value":"82"},{"type":"html","content":"<span class='clj-long'>88</span>","value":"88"},{"type":"html","content":"<span class='clj-long'>93</span>","value":"93"},{"type":"html","content":"<span class='clj-long'>96</span>","value":"96"},{"type":"html","content":"<span class='clj-long'>101</span>","value":"101"}],"value":"#{3 9 12 15 20 25 27 32 34 37 38 40 43 47 50 54 55 59 61 66 70 74 80 82 88 93 96 101}"}
;; <=
;; **
;;; ##Play game
;; **
;; @@
; Determin win based on if there is an intersection of the win set and the spaces touched by the "pawn" in a game
(defn play-game [win-set board-length]
(let [rnd-seq (rnd-seq-sum board-length)]
(not (empty? (clojure.set/intersection win-set rnd-seq)))))
(play-game #{4 5 6} 10)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-unkown'>true</span>","value":"true"}
;; <=
;; **
;;; ##Count wins
;;;
;; **
;; @@
(defn percent-win [count win-set board-length]
(let [wins
(reduce (fn [acc _]
(if (play-game win-set board-length)
(inc acc)
acc))
0 (range count))
ratio (/ wins count)]
(* 100.0 ratio)
))
(percent-win 10000 #{4 5 6} 10)
(percent-win 10000 #{5 6 7} 10)
(percent-win 10000 #{7 8 9} 10)
(percent-win 10000 #{8 9 10} 10)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-double'>70.30999999999999</span>","value":"70.30999999999999"}
;; <=
;; **
;;; ##Plot
;;; Show results for contiguous selections. X axis is the first item in the sequence. That is for x=10 the winning set is [10 11 12]
;; **
;; @@
(def data (map #(percent-win 1000 (into #{} (range % (+ 3 %))) 100) (range 1 100)))
data
(plot/list-plot data :symbol-size 20)
;; @@
;; =>
;;; {"type":"vega","content":{"width":400,"height":247.2187957763672,"padding":{"top":10,"left":55,"bottom":40,"right":10},"data":[{"name":"9575f936-ddf4-49f6-a311-a64d4a25f846","values":[{"x":0,"y":50.8},{"x":1,"y":57.3},{"x":2,"y":67.30000000000001},{"x":3,"y":77.3},{"x":4,"y":75.3},{"x":5,"y":72.0},{"x":6,"y":66.2},{"x":7,"y":71.7},{"x":8,"y":73.7},{"x":9,"y":73.4},{"x":10,"y":69.8},{"x":11,"y":70.7},{"x":12,"y":70.19999999999999},{"x":13,"y":70.3},{"x":14,"y":69.89999999999999},{"x":15,"y":72.0},{"x":16,"y":72.8},{"x":17,"y":72.39999999999999},{"x":18,"y":72.1},{"x":19,"y":71.5},{"x":20,"y":70.7},{"x":21,"y":72.1},{"x":22,"y":70.0},{"x":23,"y":71.3},{"x":24,"y":70.19999999999999},{"x":25,"y":73.1},{"x":26,"y":71.89999999999999},{"x":27,"y":71.7},{"x":28,"y":70.8},{"x":29,"y":70.3},{"x":30,"y":71.89999999999999},{"x":31,"y":67.4},{"x":32,"y":72.39999999999999},{"x":33,"y":68.7},{"x":34,"y":70.0},{"x":35,"y":70.7},{"x":36,"y":72.2},{"x":37,"y":71.8},{"x":38,"y":71.3},{"x":39,"y":69.3},{"x":40,"y":71.8},{"x":41,"y":70.8},{"x":42,"y":72.2},{"x":43,"y":71.3},{"x":44,"y":70.7},{"x":45,"y":70.19999999999999},{"x":46,"y":73.3},{"x":47,"y":70.7},{"x":48,"y":71.3},{"x":49,"y":71.8},{"x":50,"y":73.0},{"x":51,"y":68.4},{"x":52,"y":69.1},{"x":53,"y":73.4},{"x":54,"y":70.8},{"x":55,"y":73.2},{"x":56,"y":68.4},{"x":57,"y":72.39999999999999},{"x":58,"y":74.7},{"x":59,"y":71.39999999999999},{"x":60,"y":73.1},{"x":61,"y":70.19999999999999},{"x":62,"y":70.7},{"x":63,"y":71.0},{"x":64,"y":72.0},{"x":65,"y":72.6},{"x":66,"y":71.7},{"x":67,"y":70.8},{"x":68,"y":71.8},{"x":69,"y":70.19999999999999},{"x":70,"y":69.69999999999999},{"x":71,"y":70.5},{"x":72,"y":71.2},{"x":73,"y":72.39999999999999},{"x":74,"y":71.1},{"x":75,"y":70.7},{"x":76,"y":72.1},{"x":77,"y":72.89999999999999},{"x":78,"y":71.39999999999999},{"x":79,"y":72.1},{"x":80,"y":73.9},{"x":81,"y":69.6},{"x":82,"y":72.39999999999999},{"x":83,"y":70.5},{"x":84,"y":71.8},{"x":85,"y":72.2},{"x":86,"y":72.3},{"x":87,"y":73.2},{"x":88,"y":70.39999999999999},{"x":89,"y":72.3},{"x":90,"y":71.3},{"x":91,"y":69.69999999999999},{"x":92,"y":70.89999999999999},{"x":93,"y":70.3},{"x":94,"y":71.8},{"x":95,"y":69.8},{"x":96,"y":76.4},{"x":97,"y":72.8},{"x":98,"y":71.6}]}],"marks":[{"type":"symbol","from":{"data":"9575f936-ddf4-49f6-a311-a64d4a25f846"},"properties":{"enter":{"x":{"scale":"x","field":"data.x"},"y":{"scale":"y","field":"data.y"},"fill":{"value":"steelblue"},"fillOpacity":{"value":1}},"update":{"shape":"circle","size":{"value":20},"stroke":{"value":"transparent"}},"hover":{"size":{"value":60},"stroke":{"value":"white"}}}}],"scales":[{"name":"x","type":"linear","range":"width","zero":false,"domain":{"data":"9575f936-ddf4-49f6-a311-a64d4a25f846","field":"data.x"}},{"name":"y","type":"linear","range":"height","nice":true,"zero":false,"domain":{"data":"9575f936-ddf4-49f6-a311-a64d4a25f846","field":"data.y"}}],"axes":[{"type":"x","scale":"x"},{"type":"y","scale":"y"}]},"value":"#gorilla_repl.vega.VegaView{:content {:width 400, :height 247.2188, :padding {:top 10, :left 55, :bottom 40, :right 10}, :data [{:name \"9575f936-ddf4-49f6-a311-a64d4a25f846\", :values ({:x 0, :y 50.8} {:x 1, :y 57.3} {:x 2, :y 67.30000000000001} {:x 3, :y 77.3} {:x 4, :y 75.3} {:x 5, :y 72.0} {:x 6, :y 66.2} {:x 7, :y 71.7} {:x 8, :y 73.7} {:x 9, :y 73.4} {:x 10, :y 69.8} {:x 11, :y 70.7} {:x 12, :y 70.19999999999999} {:x 13, :y 70.3} {:x 14, :y 69.89999999999999} {:x 15, :y 72.0} {:x 16, :y 72.8} {:x 17, :y 72.39999999999999} {:x 18, :y 72.1} {:x 19, :y 71.5} {:x 20, :y 70.7} {:x 21, :y 72.1} {:x 22, :y 70.0} {:x 23, :y 71.3} {:x 24, :y 70.19999999999999} {:x 25, :y 73.1} {:x 26, :y 71.89999999999999} {:x 27, :y 71.7} {:x 28, :y 70.8} {:x 29, :y 70.3} {:x 30, :y 71.89999999999999} {:x 31, :y 67.4} {:x 32, :y 72.39999999999999} {:x 33, :y 68.7} {:x 34, :y 70.0} {:x 35, :y 70.7} {:x 36, :y 72.2} {:x 37, :y 71.8} {:x 38, :y 71.3} {:x 39, :y 69.3} {:x 40, :y 71.8} {:x 41, :y 70.8} {:x 42, :y 72.2} {:x 43, :y 71.3} {:x 44, :y 70.7} {:x 45, :y 70.19999999999999} {:x 46, :y 73.3} {:x 47, :y 70.7} {:x 48, :y 71.3} {:x 49, :y 71.8} {:x 50, :y 73.0} {:x 51, :y 68.4} {:x 52, :y 69.1} {:x 53, :y 73.4} {:x 54, :y 70.8} {:x 55, :y 73.2} {:x 56, :y 68.4} {:x 57, :y 72.39999999999999} {:x 58, :y 74.7} {:x 59, :y 71.39999999999999} {:x 60, :y 73.1} {:x 61, :y 70.19999999999999} {:x 62, :y 70.7} {:x 63, :y 71.0} {:x 64, :y 72.0} {:x 65, :y 72.6} {:x 66, :y 71.7} {:x 67, :y 70.8} {:x 68, :y 71.8} {:x 69, :y 70.19999999999999} {:x 70, :y 69.69999999999999} {:x 71, :y 70.5} {:x 72, :y 71.2} {:x 73, :y 72.39999999999999} {:x 74, :y 71.1} {:x 75, :y 70.7} {:x 76, :y 72.1} {:x 77, :y 72.89999999999999} {:x 78, :y 71.39999999999999} {:x 79, :y 72.1} {:x 80, :y 73.9} {:x 81, :y 69.6} {:x 82, :y 72.39999999999999} {:x 83, :y 70.5} {:x 84, :y 71.8} {:x 85, :y 72.2} {:x 86, :y 72.3} {:x 87, :y 73.2} {:x 88, :y 70.39999999999999} {:x 89, :y 72.3} {:x 90, :y 71.3} {:x 91, :y 69.69999999999999} {:x 92, :y 70.89999999999999} {:x 93, :y 70.3} {:x 94, :y 71.8} {:x 95, :y 69.8} {:x 96, :y 76.4} {:x 97, :y 72.8} {:x 98, :y 71.6})}], :marks [{:type \"symbol\", :from {:data \"9575f936-ddf4-49f6-a311-a64d4a25f846\"}, :properties {:enter {:x {:scale \"x\", :field \"data.x\"}, :y {:scale \"y\", :field \"data.y\"}, :fill {:value \"steelblue\"}, :fillOpacity {:value 1}}, :update {:shape \"circle\", :size {:value 20}, :stroke {:value \"transparent\"}}, :hover {:size {:value 60}, :stroke {:value \"white\"}}}}], :scales [{:name \"x\", :type \"linear\", :range \"width\", :zero false, :domain {:data \"9575f936-ddf4-49f6-a311-a64d4a25f846\", :field \"data.x\"}} {:name \"y\", :type \"linear\", :range \"height\", :nice true, :zero false, :domain {:data \"9575f936-ddf4-49f6-a311-a64d4a25f846\", :field \"data.y\"}}], :axes [{:type \"x\", :scale \"x\"} {:type \"y\", :scale \"y\"}]}}"}
;; <=
;; **
;;; # The best choice is at locations [4 5 6]
;; **
;; @@
;; @@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment