Skip to content

Commit

Permalink
First version of the solver.
Browse files Browse the repository at this point in the history
Reads the valid words, creates a histogram for letters and ranks words.
  • Loading branch information
archy-bold committed Jan 4, 2022
0 parents commit 323004e
Show file tree
Hide file tree
Showing 4 changed files with 2,437 additions and 0 deletions.
110 changes: 110 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)

var letters = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
var validWords = []string{}
var dictionary = map[string]int{}
var histogram = map[string]int{}
var rankedWords PairList

func main() {
// Read the valid words
fmt.Println("Reading words...")
err := readValidWords()
check(err)

// Generate histogram
fmt.Println("Building histogram...")
buildHistogram()

// Rank words based on frequency
fmt.Println("Ranking words...")
rankWords()

fmt.Printf("%+v\n", histogram)
for i := 0; i < 10; i++ {
rank := rankedWords[i]
fmt.Printf("%s: %d\n", rank.Key, rank.Value)
}
}

func check(e error) {
if e != nil {
panic(e)
}
}

func readValidWords() error {
file, err := os.Open("./solutions.txt")
if err != nil {
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
word := scanner.Text()
dictionary[word] = i
validWords = append(validWords, word)
i++
}

if err := scanner.Err(); err != nil {
return err
}

return nil
}

func buildHistogram() {
histogram = make(map[string]int, len(letters))
for _, l := range letters {
histogram[l] = 0
}

// Loop through each word and check which unique letters are in the word
for _, word := range validWords {
chrs := strings.Split(word, "")

// Loop through each char and update the histogram
checkedChars := map[string]bool{}
for _, chr := range chrs {
// If we've not already processed this letter
if _, ok := checkedChars[chr]; !ok {
histogram[chr]++
checkedChars[chr] = true
}
}
}
}

func rankWords() {
rankedWords = make(PairList, len(validWords))

for _, word := range validWords {
chrs := strings.Split(word, "")
// First set the score based on the letters that exist
checkedChars := map[string]bool{}
score := 0
for _, chr := range chrs {
if _, ok := checkedChars[chr]; !ok {
score += histogram[chr]
checkedChars[chr] = true
}
}

// Add to the ranked list
rankedWords = append(rankedWords, Pair{word, score})
}

// Sort the
sort.Sort(sort.Reverse(rankedWords))
}
12 changes: 12 additions & 0 deletions pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type Pair struct {
Key string
Value int
}

type PairList []Pair

func (p PairList) Len() int { return len(p) }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
Loading

0 comments on commit 323004e

Please sign in to comment.