Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import java.util.*
  2.  
  3. /*
  4. * Algorithm:
  5. * 1. last jew in line (first to guess):
  6. * a. count number of white hats
  7. * b. if number of white hats is even:
  8. * i. return "WHITE"
  9. * c. else:
  10. * i. return "BLACK"
  11. * 2. for each thereafter:
  12. * a. count number of white hats visible
  13. * b. if the number is even:
  14. * i. if number of jews to answer "WHITE" before me is even, guess="BLACK"
  15. * ii. else guess="WHITE"
  16. * c. else:
  17. * i. if number of jews to answer "BLACK" before me is even, guess="WHITE"
  18. * ii. else guess="BLACK"
  19. * d. if first jew to guess, guessed "WHITE":
  20. * i. return $guess
  21. * e. else:
  22. * i. return opposite color of $guess
  23. */
  24.  
  25. fun Int.isEven() = this%2 == 0
  26.  
  27. class Line(private val numJewsInLine: Int) {
  28. private var line : Array<Jew>
  29.  
  30. init {
  31. line = Array(numJewsInLine) { i -> Jew(i) }
  32. }
  33.  
  34. fun getNumWhiteHatsVisibleToIndex(index: Int): Int {
  35. var count = 1
  36. for (i in 0..(index-1)) {
  37. if (line[i].hat.color == Hat.HatColors.WHITE) {
  38. count++
  39. }
  40. }
  41. return count
  42. }
  43.  
  44. fun getFirstJewGuess(): Hat.HatColors {
  45. return if (getNumWhiteHatsVisibleToIndex(numJewsInLine-1).isEven()) {
  46. Hat.HatColors.WHITE
  47. } else {
  48. Hat.HatColors.BLACK
  49. }
  50. }
  51.  
  52. fun guess(): Pair<Double, String> {
  53. val guessOutput = StringBuilder() // holds log of guesses
  54. var numJewsGuessedWhite = 0
  55. var correctGuesses = 0
  56.  
  57. var guess = getFirstJewGuess()
  58. 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")
  59. if (guess == line[numJewsInLine-1].hat.color) correctGuesses++
  60.  
  61. for (i in numJewsInLine-2 downTo 0) {
  62. guess = line[i].guessHatColor(this, numJewsGuessedWhite)
  63. if (guess == line[i].hat.color) correctGuesses++
  64. guessOutput.append("Jew number ${i+1} guess: $guess. actual color: ${line[i].hat.color} - ${if (guess == line[i].hat.color) "CORRECT" else "WRONG"}\n")
  65. if (guess == Hat.HatColors.WHITE) numJewsGuessedWhite++
  66. }
  67. return Pair(correctGuesses.toDouble()/numJewsInLine, guessOutput.toString())
  68. }
  69.  
  70. }
  71.  
  72. class Jew (val hat: Hat, private val indexInLine: Int) {
  73. fun guessHatColor(line: Line, numJewsGuessedWhiteBeforeMe: Int): Hat.HatColors {
  74. val numWhiteHatsVisibleToMe = line.getNumWhiteHatsVisibleToIndex(indexInLine)
  75. val guessColor =
  76. if (numWhiteHatsVisibleToMe.isEven()) {
  77. if (numJewsGuessedWhiteBeforeMe.isEven()) Hat.HatColors.BLACK else Hat.HatColors.WHITE
  78. } else {
  79. if (numJewsGuessedWhiteBeforeMe.isEven()) Hat.HatColors.WHITE else Hat.HatColors.BLACK
  80. }
  81.  
  82. return if (line.getFirstJewGuess() == Hat.HatColors.WHITE) guessColor else Hat().getOppositeColor(guessColor)
  83. }
  84.  
  85. constructor(indexInLine : Int) : this(Hat(), indexInLine)
  86. }
  87.  
  88. class Hat (var color : HatColors){
  89. enum class HatColors {
  90. WHITE,
  91. BLACK
  92. }
  93.  
  94. fun getOppositeColor(color: HatColors): HatColors = if (color == HatColors.WHITE) HatColors.BLACK else HatColors.WHITE
  95.  
  96. constructor() : this((if (Random().nextBoolean()) HatColors.WHITE else HatColors.BLACK))
  97. }
  98.  
  99. fun main(args : Array<String>) {
  100. val line = Line(10)
  101. val (stats, log) = line.guess()
  102.  
  103. println("Percentage of jews who guess correctly: ${(stats * 100)}%\n$log")
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement