Skip to content

Instantly share code, notes, and snippets.

@abcdw
Last active July 1, 2018 07:02
Show Gist options
  • Save abcdw/c4c588d57c18e2c4f8e4fd2710f96149 to your computer and use it in GitHub Desktop.
Save abcdw/c4c588d57c18e2c4f8e4fd2710f96149 to your computer and use it in GitHub Desktop.
(def prices
[:car :cow :cow])
(shuffle prices)
(defn init-game []
{:doors (into {} (map-indexed (fn [a b] [a b]) (shuffle prices)))
:doors-count (count prices)
;; :turn 0
})
(defn first-turn [game door-number]
(-> game
;; (update :turn inc)
(assoc :player-choice door-number)))
(defn open-door [{pc :player-choice doors :doors dc :doors-count :as game}]
(let [doors-nums (set (range 0 dc))
car-door (first (first (filter #(= :car (second %)) doors)))
cow-door (-> doors-nums
(disj pc)
(disj car-door))]
(assoc game :cow-door (rand-nth (into [] cow-door)))))
(defn switch-door [{pc :player-choice doors :doors dc :doors-count
cd :cow-door :as game} switch-door?]
(let [doors-nums (set (range 0 dc))
alternative-choice (-> doors-nums
set
(disj cd pc)
first)]
(if switch-door?
(assoc game :player-choice alternative-choice)
game)))
(defn print-game [game]
(println game)
game)
(defn won-game? [{pc :player-choice doors :doors dc :doors-count :as game}]
(let [doors-nums (set (range 0 dc))
car-door (first (first (filter #(= :car (second %)) doors)))]
(= car-door pc)))
(defn do-a-game [switch-door?]
(-> (init-game)
(first-turn (rand-int 3))
open-door
(switch-door switch-door?)
won-game?))
(->>
(map
(fn [gn] (do-a-game true)) (range 100000))
frequencies
(into (sorted-map))
println)
;; => {false 33465, true 66535}
(->>
(map
(fn [gn] (do-a-game false)) (range 100000))
frequencies
(into (sorted-map))
println)
;; => {false 66540, true 33460}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment