Skip to content

Instantly share code, notes, and snippets.

@rafd
Created February 2, 2023 01:18
Show Gist options
  • Save rafd/3e724a43b66909eea830da17bc6500ff to your computer and use it in GitHub Desktop.
Save rafd/3e724a43b66909eea830da17bc6500ff to your computer and use it in GitHub Desktop.
2023-02-01 clojure meetup
;; 2 blue balls, 6 yellow balls
;; opt a - take 3, win if there are not 2 blue
;; opt b - take 1, win if it isn't blue
;; which option better?
;;(def bag {:blueballs #{'x 'y} :yellow-balls #{'a 'b 'c 'd 'e 'f}})
;;(def bag [1 1 0 0 0 0 0 0])
(def prototype-bag
[:yellow :yellow :yellow :yellow :yellow :yellow :blue :blue])
#_(#{:blue} :x)
(defn win-option-a? [bag]
(let [pics (take 3 bag)
num-blue (count (filter #{:blue} pics))]
(not= num-blue 2)))
#_(win-option-a? [:blue :blue :yellow])
#_(win-option-a? [:blue :yellow :yellow])
#_(win-option-a? [:yellow :yellow :yellow])
;; opt b - take 1, win if it isn't blue
(defn win-option-b? [bag]
(let [ball (first bag)]
(not= ball :blue)))
#_(win-option-b? [:blue])
#_(win-option-b? [:yellow])
#_(->> (repeat 100000 prototype-bag)
(map shuffle)
(reduce (fn [[target-count total-count] item]
[(if (win-option-a? item)
(inc target-count)
target-count)
(inc total-count)])
[0 0.0])
(apply /))
#_(->> (repeat 100000 prototype-bag)
(map shuffle)
(map win-option-a?)
(filter true?)
count
(#(/ % 100000.0)))
#_(->> (repeat 100000 prototype-bag)
(map shuffle)
(map win-option-b?)
(filter true?)
count
(#(/ % 100000.0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment