Skip to content

Instantly share code, notes, and snippets.

@TeWu
Last active October 4, 2016 13:05
Show Gist options
  • Save TeWu/9d40a5c228ca584321b320f8c798bd35 to your computer and use it in GitHub Desktop.
Save TeWu/9d40a5c228ca584321b320f8c798bd35 to your computer and use it in GitHub Desktop.
The prisoner boxes riddle solution simulation
#
# https://www.youtube.com/watch?v=vIdStMTgNl0
#
$SUCCESS = 0
$M = 1_000_000
$N = 10
puts Time.now
def graph_method(i, tries)
(1..tries).inject([i, false]) do |a,e|
c = $BOXES[a[0]]
[ c, a[1] || c == i]
end[1]
end
$M.times do |m|
$BOXES = (0..($N-1)).to_a.shuffle
#puts $BOXES.inspect
result = (0..($N-1)).map do |i|
graph_method(i, 5)
end.to_a.none?{|x| !x }
$SUCCESS += 1 if result
end
puts $M
puts $SUCCESS
puts $SUCCESS / $M.to_f
### SAMPLE OUTPUT:
# 2016-10-04 15:03:06 +0200
# 1000000
# 354068
# 0.354068
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment