fallout.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. )
  8. type Game struct {
  9. Words map[string]*Word
  10. }
  11. type Word struct {
  12. Word string
  13. Score int
  14. }
  15. func main() {
  16. game := NewGame()
  17. err := game.getWordsFromStdin()
  18. if err != nil {
  19. fmt.Printf("Error getting Words from stdin: %s\n", err)
  20. os.Exit(1)
  21. }
  22. err = game.scoreWordsByCommonLetterLocations()
  23. if err != nil {
  24. fmt.Printf("Error scoring Words: %s\n", err)
  25. os.Exit(1)
  26. }
  27. err = game.printSortedScores()
  28. if err != nil {
  29. fmt.Printf("Error printing sorted scores: %s\n", err)
  30. os.Exit(1)
  31. }
  32. // get guess and Score from stdin
  33. // remove Words that don't match guess and Score
  34. }
  35. func NewGame() *Game {
  36. return &Game{
  37. Words: make(map[string]*Word),
  38. }
  39. }
  40. func (g Game) printSortedScores() error {
  41. var sortedWordScores []string
  42. for _, word := range g.Words {
  43. sortedWordScores = append(sortedWordScores, word.Word)
  44. }
  45. // sort words by score
  46. sort.Slice(sortedWordScores, func(i, j int) bool {
  47. return g.Words[sortedWordScores[i]].Score > g.Words[sortedWordScores[j]].Score
  48. })
  49. fmt.Print("\nWords sorted by Score:\n")
  50. for _, word := range sortedWordScores {
  51. fmt.Printf("%s: %d\n", word, g.Words[word].Score)
  52. }
  53. fmt.Println("")
  54. return nil
  55. }
  56. func (g Game) scoreWordsByCommonLetterLocations() error {
  57. letterIdxScores := make(map[int]map[string]int)
  58. var lastWord string
  59. for _, word := range g.Words {
  60. word.Score = 0
  61. lastWord = word.Word
  62. }
  63. for idx, _ := range lastWord {
  64. letterIdxScores[idx] = make(map[string]int)
  65. }
  66. for _, word := range g.Words {
  67. for idx, letter := range word.Word {
  68. letterIdxScores[idx][string(letter)]++
  69. }
  70. }
  71. for _, word := range g.Words {
  72. for idx, letter := range word.Word {
  73. word.Score += letterIdxScores[idx][string(letter)]
  74. }
  75. }
  76. return nil
  77. }
  78. func (g Game) getWordsFromStdin() error {
  79. fmt.Println("Enter Words, one per line. Enter a period to end input.")
  80. wordLen := 0
  81. for {
  82. reader := bufio.NewReader(os.Stdin)
  83. word, _ := reader.ReadString('\n')
  84. word = word[:len(word)-1]
  85. if word == "." {
  86. fmt.Println("got Words!")
  87. break
  88. }
  89. if wordLen == 0 {
  90. wordLen = len(word)
  91. } else {
  92. if len(word) != wordLen {
  93. fmt.Printf("Error: All words must be the same length (%d), skipping: %s\n", wordLen, word)
  94. continue
  95. }
  96. }
  97. g.Words[word] = &Word{Word: word}
  98. }
  99. return nil
  100. }