Guest User

Go Code

a guest
Mar 18th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.03 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "io/ioutil"
  7.     "math"
  8.     "runtime"
  9.     "sort"
  10.     "strings"
  11.     "sync"
  12.     "time"
  13. )
  14.  
  15. func main() {
  16.     start := time.Now()
  17.     possible_answers := read("./wordle_min.json")
  18.     score_index := make([]int, len(possible_answers))
  19.  
  20.     thread_count := runtime.NumCPU()
  21.     chunk_size := int(math.Ceil(float64(len(possible_answers)) / float64(thread_count)))
  22.  
  23.     var wg sync.WaitGroup
  24.     wg.Add(thread_count)
  25.     ch := make(chan []int, thread_count)
  26.  
  27.     for chunk := 0; chunk < thread_count; chunk++ {
  28.         from := chunk_size * chunk
  29.         to := 0
  30.         if (from + chunk_size) > len(possible_answers) {
  31.             to = len(possible_answers)
  32.         } else {
  33.             to = from + chunk_size
  34.         }
  35.  
  36.         go func() {
  37.             si := make([]int, len(possible_answers))
  38.             for x := from; x < to; x++{
  39.                 for y := range possible_answers {
  40.                     for x_char_index := 0; x_char_index < 5; x_char_index++ {
  41.                         x_char := possible_answers[x][x_char_index]
  42.                         if x_char == possible_answers[y][x_char_index] {
  43.                             si[y] += 5
  44.                         } else if strings.Contains(possible_answers[y], string(x_char)) {
  45.                             si[y] += 4
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             ch <- si
  51.             wg.Done()
  52.         }()
  53.     }
  54.     wg.Wait()
  55.     close(ch)
  56.  
  57.     for result := range ch {
  58.         for i, v := range result {
  59.             score_index[i] += v
  60.         }
  61.     }
  62.    
  63.  
  64.     sorted_scores := make([]int, len(possible_answers))
  65.     copy(sorted_scores, score_index)
  66.     sort.Sort(sort.Reverse(sort.IntSlice(sorted_scores)))
  67.    
  68.     res := fmt.Sprintf("Best Words:\n > ")
  69.     for i := 0; i < 5; i++ {
  70.         score := sorted_scores[i]
  71.         res += fmt.Sprintf("%v: %v | ", possible_answers[find_index(score_index, score)], score)
  72.     }
  73.     fmt.Println(res)
  74.     fmt.Printf("Process took %v ms", time.Since(start).Milliseconds())
  75. }
  76.  
  77. func read(path string) []string {
  78.     content, _ := ioutil.ReadFile(path)
  79.     var data []string
  80.     json.Unmarshal(content, &data)
  81.     return data
  82. }
  83.  
  84. func find_index(arr []int, value int) int {
  85.     index := -1
  86.     for i, v := range arr {
  87.         if v == value {
  88.             index = i
  89.             break
  90.         }
  91.     }
  92.     return index
  93. }
Advertisement
Add Comment
Please, Sign In to add comment