Skip to content

Instantly share code, notes, and snippets.

@Timmeey
Last active November 14, 2020 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Timmeey/02ed329b001bef4fb0b76ca662cd91e8 to your computer and use it in GitHub Desktop.
Save Timmeey/02ed329b001bef4fb0b76ca662cd91e8 to your computer and use it in GitHub Desktop.
ThreeDoorPuzzle(ZONK), what happens if you have to chose 1 out of three doors? Only one is correct. Once you have chosen a door. The Showmaster is going to opens all the other doors, but the correct one, XOR (if you already chose the correct door) leaves one incorrect one closed.
import java.util.*
import kotlin.math.roundToInt
fun main(){
val intendedGames = 100000
var reconsidered=0
var games=0
var reconsideredCorrect=0
var notReconsideredCorrect=0
var gameArray = listOf(
Door(correct = false, shown = false),
Door(true,false),
Door(false,false))
while(games < intendedGames){
games++
gameArray = gameArray.shuffled().also{it.forEach{it.reset()}}
val chosenDoor=gameArray.first()
if(Math.random()>0.5){
reconsidered++
gameArray.filter { it != chosenDoor }
.filter{ !it.correct }
.takeLast(gameArray.size-2)
.forEach{it.show()
}
val reconsideredDoor = gameArray.first { it!=chosenDoor && !it.shown }
if(reconsideredDoor.correct) {
reconsideredCorrect++
}
}else {
if(chosenDoor.correct){
notReconsideredCorrect++
}
}
gameArray.forEach { it.reset() }
}
println ("Games: $games, recosidered: $reconsidered")
println ("ReconsideredCorrect: ${((reconsideredCorrect.toDouble()/reconsidered.toDouble())*100).roundToInt()}% ($reconsideredCorrect)")
println ("Not reconsidered correct: ${((notReconsideredCorrect.toDouble()/(games-reconsidered.toDouble()))*100).roundToInt()}% $notReconsideredCorrect")
} data class Door(val correct:Boolean,
var shown:Boolean,
val id: UUID = UUID.randomUUID()
){
fun reset(){shown=false}
fun show(){shown=true}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment