Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*
- /*
- * Algorithm:
- * 1. last jew in line (first to guess):
- * a. count number of white hats
- * b. if number of white hats is even:
- * i. return "WHITE"
- * c. else:
- * i. return "BLACK"
- * 2. for each thereafter:
- * a. count number of white hats visible
- * b. if the number is even:
- * i. if number of jews to answer "WHITE" before me is even, guess="BLACK"
- * ii. else guess="WHITE"
- * c. else:
- * i. if number of jews to answer "BLACK" before me is even, guess="WHITE"
- * ii. else guess="BLACK"
- * d. if first jew to guess, guessed "WHITE":
- * i. return $guess
- * e. else:
- * i. return opposite color of $guess
- */
- fun Int.isEven() = this%2 == 0
- class Line(private val numJewsInLine: Int) {
- private var line : Array<Jew>
- init {
- line = Array(numJewsInLine) { i -> Jew(i) }
- }
- fun getNumWhiteHatsVisibleToIndex(index: Int): Int {
- var count = 1
- for (i in 0..(index-1)) {
- if (line[i].hat.color == Hat.HatColors.WHITE) {
- count++
- }
- }
- return count
- }
- fun getFirstJewGuess(): Hat.HatColors {
- return if (getNumWhiteHatsVisibleToIndex(numJewsInLine-1).isEven()) {
- Hat.HatColors.WHITE
- } else {
- Hat.HatColors.BLACK
- }
- }
- fun guess(): Pair<Double, String> {
- val guessOutput = StringBuilder() // holds log of guesses
- var numJewsGuessedWhite = 0
- var correctGuesses = 0
- var guess = getFirstJewGuess()
- guessOutput.append("Jew number $numJewsInLine guess: $guess. actual color: ${line[numJewsInLine-1].hat.color} - ${if (guess == line[numJewsInLine-1].hat.color) "CORRECT" else "WRONG"}\n")
- if (guess == line[numJewsInLine-1].hat.color) correctGuesses++
- for (i in numJewsInLine-2 downTo 0) {
- guess = line[i].guessHatColor(this, numJewsGuessedWhite)
- if (guess == line[i].hat.color) correctGuesses++
- guessOutput.append("Jew number ${i+1} guess: $guess. actual color: ${line[i].hat.color} - ${if (guess == line[i].hat.color) "CORRECT" else "WRONG"}\n")
- if (guess == Hat.HatColors.WHITE) numJewsGuessedWhite++
- }
- return Pair(correctGuesses.toDouble()/numJewsInLine, guessOutput.toString())
- }
- }
- class Jew (val hat: Hat, private val indexInLine: Int) {
- fun guessHatColor(line: Line, numJewsGuessedWhiteBeforeMe: Int): Hat.HatColors {
- val numWhiteHatsVisibleToMe = line.getNumWhiteHatsVisibleToIndex(indexInLine)
- val guessColor =
- if (numWhiteHatsVisibleToMe.isEven()) {
- if (numJewsGuessedWhiteBeforeMe.isEven()) Hat.HatColors.BLACK else Hat.HatColors.WHITE
- } else {
- if (numJewsGuessedWhiteBeforeMe.isEven()) Hat.HatColors.WHITE else Hat.HatColors.BLACK
- }
- return if (line.getFirstJewGuess() == Hat.HatColors.WHITE) guessColor else Hat().getOppositeColor(guessColor)
- }
- constructor(indexInLine : Int) : this(Hat(), indexInLine)
- }
- class Hat (var color : HatColors){
- enum class HatColors {
- WHITE,
- BLACK
- }
- fun getOppositeColor(color: HatColors): HatColors = if (color == HatColors.WHITE) HatColors.BLACK else HatColors.WHITE
- constructor() : this((if (Random().nextBoolean()) HatColors.WHITE else HatColors.BLACK))
- }
- fun main(args : Array<String>) {
- val line = Line(10)
- val (stats, log) = line.guess()
- println("Percentage of jews who guess correctly: ${(stats * 100)}%\n$log")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement