Guest User

V Code

a guest
Mar 18th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.54 KB | Source Code | 0 0
  1. import json
  2. import os
  3. import time
  4. import math
  5. import runtime
  6.  
  7. fn main() {
  8.     println('Started...')
  9.     sw := time.new_stopwatch()
  10.  
  11.     possible_answers := json.decode([]string, os.read_file('./wordle_min.json')!)!
  12.  
  13.     mut score_index := []int{len: possible_answers.len, init: 0}
  14.     thread_count := runtime.nr_cpus()
  15.     chunk_size := int(math.ceil(f64(possible_answers.len) / f64(thread_count)))
  16.  
  17.     ch := chan []int{}
  18.  
  19.     for chunk in 0 .. thread_count {
  20.         from := chunk_size * chunk
  21.         to := if (from + chunk_size) > possible_answers.len {
  22.             possible_answers.len
  23.         } else {
  24.             from + chunk_size
  25.         }
  26.  
  27.         spawn add_scores(possible_answers, from, to, ch)
  28.     }
  29.  
  30.     for _ in 0 .. thread_count {
  31.         result := <-ch
  32.         for i, v in result {
  33.             score_index[i] += v
  34.         }
  35.     }
  36.     ch.close()
  37.  
  38.     mut sorted_scores := score_index.clone()
  39.     sorted_scores.sort(a > b)
  40.     mut res := 'Best Words:\n > '
  41.     for i in 0 .. 5 {
  42.         score := sorted_scores[i]
  43.         res += '${possible_answers[score_index.index(score)]}: ${score} | '
  44.     }
  45.  
  46.     println(res)
  47.  
  48.     println('Process took ${sw.elapsed().milliseconds()} ms')
  49. }
  50.  
  51. fn add_scores(possible_answers []string, from int, to int, ch chan []int) {
  52.     mut res := []int{len: possible_answers.len, init: 0}
  53.  
  54.     for x in from .. to {
  55.         for y in 0 .. possible_answers.len {
  56.             for x_char_index in 0 .. 5 {
  57.                 x_char := possible_answers[x][x_char_index]
  58.                 if x_char == possible_answers[y][x_char_index] {
  59.                     res[y] += 5
  60.                 } else if possible_answers[y].contains(x_char.ascii_str()) {
  61.                     res[y] += 4
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     ch <- res
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment