colour-algo.go (1497B)
1 package algos 2 3 import "git.hanabi.in/dev/wordle-cli/src/colours" 4 5 type Lookup map[string]int 6 7 // Generate answer lookup table, and fill up with count of letters. 8 func GenAnsLookup(answer string) Lookup { 9 ans_lookup := make(Lookup, 0) 10 for _, ans_elem := range answer { 11 ans_char := string(ans_elem) 12 ans_lookup[ans_char]++ 13 } 14 return ans_lookup 15 } 16 17 // Sahiba's algo -- commit your old code sk, I will patch the refactoring later -- ac. 18 // Get colours of the guess + the alphabet colouring too. 19 func GetColours(answer, guess string, ans_lookup, alphabet Lookup) string { 20 // answer and guess required to check for green letters. 21 var res string 22 guess_lookup_table := make(Lookup, 0) 23 for idx, guess_elem := range guess { 24 guess_char := string(guess_elem) 25 if ans_lookup[guess_char] == 0 { // Letter does not exist in answer, red. 26 res += "R" 27 alphabet[guess_char] = colours.Code_red 28 } else { // If letter exists. 29 guess_lookup_table[guess_char]++ 30 if guess_lookup_table[guess_char] <= ans_lookup[guess_char] { 31 if answer[idx] == guess[idx] { 32 res += "G" 33 alphabet[guess_char] = colours.Code_green 34 } else { 35 res += "Y" 36 if alphabet[guess_char] == colours.Code_grey { // Don't overwrite a Green alphabet with Yellow. 37 alphabet[guess_char] = colours.Code_yellow 38 } 39 } 40 } else { // Extra repeating characters. 41 // alphabet colour would have been at least yellow, because it matched already, so don't edit. 42 res += "R" 43 } 44 } 45 } 46 return res 47 }