fallout.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // get guess and Score from stdin
  35. guess, score := getGuessAndScoreFromStdin()
  36. fmt.Printf("Guess: %s, Score: %d\n", guess, score)
  37. game.FilterWords(guess, score)
  38. err = game.printSortedScores()
  39. if err != nil {
  40. fmt.Printf("Error printing sorted scores: %s\n", err)
  41. os.Exit(1)
  42. }
  43. if len(game.Words) == 1 {
  44. for word := range game.Words {
  45. fmt.Printf("The word is: %s\n", word)
  46. os.Exit(0)
  47. }
  48. }
  49. }
  50. // iterate through multiple turns
  51. }
  52. func NewGame() *Game {
  53. return &Game{
  54. Words: make(map[string]*Word),
  55. }
  56. }
  57. func (g Game) FilterWords(guess string, score int) {
  58. for _, word := range g.Words {
  59. if !word.MatchesGuess(guess, score) {
  60. delete(g.Words, word.Word)
  61. }
  62. }
  63. }
  64. func (w Word) MatchesGuess(guess string, numMatchingChars int) bool {
  65. score := 0
  66. for idx, letter := range w.Word {
  67. if string(letter) == string(guess[idx]) {
  68. score++
  69. }
  70. }
  71. return score == numMatchingChars
  72. }
  73. func getGuessAndScoreFromStdin() (string, int) {
  74. reader := bufio.NewReader(os.Stdin)
  75. fmt.Print("Enter Guess: ")
  76. guess, _ := reader.ReadString('\n')
  77. guess = guess[:len(guess)-1]
  78. fmt.Print("Enter Score: ")
  79. var score int
  80. _, err := fmt.Scanf("%d", &score)
  81. if err != nil {
  82. fmt.Printf("Error reading score: %s\n", err)
  83. os.Exit(1)
  84. }
  85. return guess, score
  86. }
  87. func (g Game) printSortedScores() error {
  88. var sortedWordScores []string
  89. for _, word := range g.Words {
  90. sortedWordScores = append(sortedWordScores, word.Word)
  91. }
  92. // sort words by score
  93. sort.Slice(sortedWordScores, func(i, j int) bool {
  94. return g.Words[sortedWordScores[i]].Score > g.Words[sortedWordScores[j]].Score
  95. })
  96. fmt.Println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  97. fmt.Print("Words sorted by Score:\n")
  98. for _, word := range sortedWordScores {
  99. fmt.Printf("%s: %d\n", word, g.Words[word].Score)
  100. }
  101. fmt.Println("")
  102. return nil
  103. }
  104. func (g Game) scoreWordsByCommonLetterLocations() error {
  105. letterIdxScores := make(map[int]map[string]int)
  106. var lastWord string
  107. for _, word := range g.Words {
  108. word.Score = 0
  109. lastWord = word.Word
  110. }
  111. for idx := range lastWord {
  112. letterIdxScores[idx] = make(map[string]int)
  113. }
  114. for _, word := range g.Words {
  115. for idx, letter := range word.Word {
  116. letterIdxScores[idx][string(letter)]++
  117. }
  118. }
  119. for _, word := range g.Words {
  120. for idx, letter := range word.Word {
  121. word.Score += letterIdxScores[idx][string(letter)]
  122. }
  123. }
  124. return nil
  125. }
  126. func (g Game) getWordsFromStdin() error {
  127. fmt.Println("Enter Words, one per line. Enter a period to end input.")
  128. for {
  129. reader := bufio.NewReader(os.Stdin)
  130. word, _ := reader.ReadString('\n')
  131. word = word[:len(word)-1]
  132. if word == "." {
  133. fmt.Println("got Words!")
  134. break
  135. }
  136. if g.Length == 0 {
  137. g.Length = len(word)
  138. } else {
  139. if len(word) != g.Length {
  140. fmt.Printf("Error: All words must be the same length (%d), skipping: %s\n", g.Length, word)
  141. continue
  142. }
  143. }
  144. g.Words[word] = &Word{Word: word}
  145. }
  146. return nil
  147. }