fallout.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. )
  8. type Game struct {
  9. Words map[string]*Word
  10. Length int
  11. }
  12. type Word struct {
  13. Word string
  14. Score int
  15. }
  16. func main() {
  17. game := NewGame()
  18. err := game.getWordsFromStdin()
  19. if err != nil {
  20. fmt.Printf("Error getting Words from stdin: %s\n", err)
  21. os.Exit(1)
  22. }
  23. err = game.scoreWordsByCommonLetterLocations()
  24. if err != nil {
  25. fmt.Printf("Error scoring Words: %s\n", err)
  26. os.Exit(1)
  27. }
  28. _, err = game.printSortedScores()
  29. if err != nil {
  30. fmt.Printf("Error printing sorted scores: %s\n", err)
  31. os.Exit(1)
  32. }
  33. for i := 0; i < 3; i++ {
  34. guess, score := getGuessAndScoreFromStdin()
  35. fmt.Printf("Guess: %s, Score: %d\n", guess, score)
  36. game.FilterWords(guess, score)
  37. err = game.scoreWordsByCommonLetterLocations()
  38. if err != nil {
  39. fmt.Printf("Error scoring Words: %s\n", err)
  40. os.Exit(1)
  41. }
  42. _, err = game.printSortedScores()
  43. if err != nil {
  44. fmt.Printf("Error printing sorted scores: %s\n", err)
  45. os.Exit(1)
  46. }
  47. if len(game.Words) == 1 {
  48. for word := range game.Words {
  49. fmt.Printf("The word is: %s\n", word)
  50. os.Exit(0)
  51. }
  52. }
  53. }
  54. }
  55. func NewGame() *Game {
  56. return &Game{
  57. Words: make(map[string]*Word),
  58. }
  59. }
  60. func (g Game) FilterWords(guess string, score int) {
  61. for _, word := range g.Words {
  62. if !word.MatchesGuess(guess, score) {
  63. delete(g.Words, word.Word)
  64. }
  65. }
  66. }
  67. func (w Word) MatchesGuess(guess string, numMatchingChars int) bool {
  68. score := 0
  69. for idx, letter := range w.Word {
  70. if string(letter) == string(guess[idx]) {
  71. score++
  72. }
  73. }
  74. return score == numMatchingChars
  75. }
  76. func getGuessAndScoreFromStdin() (string, int) {
  77. reader := bufio.NewReader(os.Stdin)
  78. fmt.Print("Enter Guess: ")
  79. guess, _ := reader.ReadString('\n')
  80. guess = guess[:len(guess)-1]
  81. fmt.Print("Enter Score: ")
  82. var score int
  83. _, err := fmt.Scanf("%d", &score)
  84. if err != nil {
  85. fmt.Printf("Error reading score: %s\n", err)
  86. os.Exit(1)
  87. }
  88. return guess, score
  89. }
  90. func (g Game) printSortedScores() (string, error) {
  91. sortedWordScores := g.getSortedScores()
  92. fmt.Println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  93. fmt.Print("Words sorted by Score:\n")
  94. for _, word := range sortedWordScores {
  95. fmt.Printf("%s: %d\n", word, g.Words[word].Score)
  96. }
  97. fmt.Println("")
  98. return sortedWordScores[0], nil
  99. }
  100. func (g Game) getSortedScores() []string {
  101. var sortedWordScores []string
  102. for _, word := range g.Words {
  103. sortedWordScores = append(sortedWordScores, word.Word)
  104. }
  105. // sort words by score
  106. sort.Slice(sortedWordScores, func(i, j int) bool {
  107. return g.Words[sortedWordScores[i]].Score > g.Words[sortedWordScores[j]].Score
  108. })
  109. return sortedWordScores
  110. }
  111. func (g Game) getBestGuess() (string, error) {
  112. return g.getSortedScores()[0], nil
  113. }
  114. func (g Game) scoreWordsByCommonLetterLocations() error {
  115. letterIdxScores := make(map[int]map[string]int)
  116. var lastWord string
  117. for _, word := range g.Words {
  118. word.Score = 0
  119. lastWord = word.Word
  120. }
  121. for idx := range lastWord {
  122. letterIdxScores[idx] = make(map[string]int)
  123. }
  124. for _, word := range g.Words {
  125. for idx, letter := range word.Word {
  126. letterIdxScores[idx][string(letter)]++
  127. }
  128. }
  129. for _, word := range g.Words {
  130. for idx, letter := range word.Word {
  131. word.Score += letterIdxScores[idx][string(letter)]
  132. }
  133. }
  134. return nil
  135. }
  136. func (g Game) getWordsFromStdin() error {
  137. fmt.Println("Enter Words, one per line. Enter a period to end input.")
  138. for {
  139. reader := bufio.NewReader(os.Stdin)
  140. word, _ := reader.ReadString('\n')
  141. word = word[:len(word)-1]
  142. if word == "." {
  143. fmt.Println("got Words!")
  144. break
  145. }
  146. if g.Length == 0 {
  147. g.Length = len(word)
  148. } else {
  149. if len(word) != g.Length {
  150. fmt.Printf("Error: All words must be the same length (%d), skipping: %s\n", g.Length, word)
  151. continue
  152. }
  153. }
  154. g.Words[word] = &Word{Word: word}
  155. }
  156. return nil
  157. }