Skip to content

Instantly share code, notes, and snippets.

@Timmeey
Last active November 14, 2020 16:42
Show Gist options
  • Save Timmeey/02ed329b001bef4fb0b76ca662cd91e8 to your computer and use it in GitHub Desktop.
Save Timmeey/02ed329b001bef4fb0b76ca662cd91e8 to your computer and use it in GitHub Desktop.

Revisions

  1. Timmeey revised this gist Nov 14, 2020. No changes.
  2. Timmeey created this gist Nov 14, 2020.
    54 changes: 54 additions & 0 deletions ThreeDoorPuzzle.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    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}
    }