Created
July 1, 2022 21:31
-
-
Save pkulak/db8a1242f4e709051a71058a45c75357 to your computer and use it in GitHub Desktop.
100 Prisoners Riddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
var allUnder = 0 | |
var anyOver = 0 | |
repeat(500_000) { | |
val boxes = (0 until 100).toList().shuffled() | |
if (allUnderFifty(boxes)) { | |
allUnder++ | |
} else { | |
anyOver++ | |
} | |
} | |
println(allUnder.toDouble() / (anyOver + allUnder).toDouble()) | |
} | |
fun allUnderFifty(boxes: List<Int>): Boolean { | |
(0 until 100).forEach { | |
if (findYourNumber(boxes, it) > 50) { | |
return false | |
} | |
} | |
return true | |
} | |
fun findYourNumber(boxes: List<Int>, number: Int): Int { | |
var counter = 0 | |
var n = number | |
while (true) { | |
counter++ | |
n = boxes[n] | |
if (n == number) { | |
return counter | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment