Advertisement
Guest User

Finding the longest word string of champion names

a guest
Nov 27th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.23 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bytes"
  5.     "fmt"
  6.     "math"
  7.     "math/rand"
  8.     "time"
  9. )
  10.  
  11. type DecisionNode struct {
  12.     position      int
  13.     totalPosition int
  14.     word          string
  15.     result        string
  16. }
  17.  
  18. type Result struct {
  19.     value     string
  20.     linkCount int
  21.     length    int
  22. }
  23.  
  24. var list []string = []string{"aatrox", "ahri", "akali", "alistar", "amumu", "anivia", "annie", "ashe", "azir", "blitzcrank", "brand", "braum", "caitlyn", "cassiopeia", "chogath", "corki", "darius", "diana", "drmundo", "draven", "elise", "evelynn", "ezreal", "fiddlesticks", "fiora", "fizz", "galio", "gangplank", "garen", "gnar", "gragas", "graves", "hecarim", "heimerdinger", "irelia", "janna", "jarvaniv", "jax", "jayce", "jinx", "kalista", "karma", "karthus", "kassadin", "katarina", "kayle", "kennen", "khazix", "kogmaw", "leblanc", "leesin", "leona", "lissandra", "lucian", "lulu", "lux", "malphite", "malzahar", "maokai", "masteryi", "missfortune", "mordekaiser", "morgana", "nami", "nasus", "nautilus", "nidalee", "nocturne", "nunu", "olaf", "orianna", "pantheon", "poppy", "quinn", "rammus", "renekton", "rengar", "riven", "rumble", "ryze", "sejuani", "shaco", "shen", "shyvana", "singed", "sion", "sivir", "skarner", "sona", "soraka", "swain", "syndra", "talon", "taric", "teemo", "thresh", "tristana", "trundle", "tryndamere", "twistedfate", "twitch", "udyr", "urgot", "varus", "vayne", "veigar", "velkoz", "vi", "viktor", "vladimir", "volibear", "warwick", "wukong", "xerath", "xinzhao", "yasuo", "yorick", "zac", "zed", "ziggs", "zilean", "zyra"}
  25. var decisions DecisionNode
  26. var used map[string]bool
  27. var bestResults []Result
  28. var timer int
  29. var progressTimer int
  30. var progressTicksSinceBetterFound int
  31. var timerInitial int = 50000
  32.  
  33. var bestResultLinkCount int = 0
  34. var bestResultLength int = math.MaxInt32
  35.  
  36. func main() {
  37.     rand.Seed(time.Now().UTC().UnixNano())
  38.     timer = timerInitial
  39.     run()
  40. }
  41.  
  42. func run() {
  43.     initialDecision := &decisions
  44.     for {
  45.         // Clear the map.
  46.         used = make(map[string]bool)
  47.         initialDecision.position = 0
  48.         initialDecision.totalPosition = 0
  49.         i := rand.Intn(len(list))
  50.         initialDecision.word = list[i]
  51.         initialDecision.result = list[i]
  52.         used[list[i]] = true
  53.         bruteForce(initialDecision, 0)
  54.     }
  55.     fmt.Println("Best Results: ", len(bestResults))
  56.     fmt.Println(bestResults)
  57. }
  58.  
  59. func bruteForce(decision *DecisionNode, linkCount int) bool {
  60.     progressTimer++
  61.     if progressTimer == 10000 {
  62.         fmt.Println("Progress: ", decision.result, timer)
  63.         progressTimer = 0
  64.         progressTicksSinceBetterFound++
  65.     }
  66.  
  67.     timer--
  68.     if timer == 0 {
  69.         fmt.Println("Reseeding, was on", decision.result, timer)
  70.         timer = timerInitial
  71.         return true
  72.     }
  73.  
  74.     linkCount++
  75.     length := len(decision.result)
  76.  
  77.     if bestResultLinkCount < linkCount {
  78.         // Found a better link count
  79.         bestResults = nil
  80.         bestResultLinkCount = linkCount
  81.         bestResultLength = length
  82.         timer += 2000 * progressTicksSinceBetterFound
  83.         fmt.Println("Found better link count: ", saveResult(decision.result, linkCount), timer)
  84.         progressTicksSinceBetterFound = 0
  85.     } else if bestResultLinkCount == linkCount {
  86.         if bestResultLength >= length {
  87.             // Found at least as good of a length
  88.             better := false
  89.             if bestResultLength > length {
  90.                 // This length is better - clear the old results
  91.                 better = true
  92.                 bestResults = nil
  93.             }
  94.             bestResultLength = length
  95.             result := saveResult(decision.result, linkCount)
  96.             timer += 2000 * progressTicksSinceBetterFound
  97.             if better {
  98.                 fmt.Println("Found better length: ", result, timer)
  99.             }
  100.             progressTicksSinceBetterFound = 0
  101.         }
  102.     }
  103.  
  104.     var currentList []string = make([]string, len(list))
  105.     copy(currentList, list)
  106.     currentList = removeUsedWords(currentList)
  107.  
  108.     positionsPerm := rand.Perm(len(decision.word))
  109.  
  110.     for i := 0; i < len(positionsPerm); i++ {
  111.         decision.position = positionsPerm[i]
  112.         indexInNextWord := 0
  113.         skipPosition := false
  114.         var newList []string = make([]string, len(currentList))
  115.         copy(newList, currentList)
  116.         for j := decision.position; j < len(decision.word); j++ {
  117.             newSlice := getByLetter(newList, decision.word[j], indexInNextWord)
  118.             newSlice = removeUsedWords(newSlice)
  119.             if len(newSlice) > 0 {
  120.                 newList = newSlice
  121.             } else {
  122.                 // Couldn't find any word that fit starting at decision.position
  123.                 indexInNextWord = 0
  124.                 skipPosition = true
  125.                 break
  126.             }
  127.             indexInNextWord++
  128.         }
  129.         if !skipPosition {
  130.             newWordPerm := rand.Perm(len(newList))
  131.             for j := 0; j < len(newWordPerm); j++ {
  132.                 word := newList[newWordPerm[j]]
  133.                 used[word] = true
  134.  
  135.                 nextDecision := DecisionNode{}
  136.                 nextDecision.word = word
  137.                 nextDecision.totalPosition = decision.totalPosition + decision.position
  138.                 nextDecision.result = concat(decision.result, nextDecision.word, nextDecision.totalPosition)
  139.  
  140.                 if bruteForce(&nextDecision, linkCount) {
  141.                     return true
  142.                 }
  143.  
  144.                 delete(used, word)
  145.             }
  146.         }
  147.         decision.position++
  148.         if decision.position == len(decision.word) {
  149.             break
  150.         }
  151.     }
  152.     return false
  153. }
  154.  
  155. func concat(s1 string, s2 string, positionInS1 int) string {
  156.     var buffer bytes.Buffer
  157.  
  158.     buffer.WriteString(s1[:positionInS1])
  159.     buffer.WriteString(s2)
  160.  
  161.     return buffer.String()
  162. }
  163.  
  164. func getByLetter(slice []string, letter uint8, index int) []string {
  165.     first := -1
  166.     for i := 0; i < len(slice); i++ {
  167.         if len(slice[i]) <= index {
  168.             continue
  169.         }
  170.         if slice[i][index] == letter {
  171.             first = i
  172.             break
  173.         }
  174.     }
  175.     if first == -1 {
  176.         return slice[:0]
  177.     }
  178.     last := first + 1
  179.     for i := first + 1; i < len(slice); i++ {
  180.         if len(slice[i]) <= index {
  181.             continue
  182.         }
  183.         if slice[i][index] != letter {
  184.             last = i
  185.             break
  186.         }
  187.     }
  188.     return slice[first:last]
  189. }
  190.  
  191. func removeUsedWords(slice []string) []string {
  192.     for {
  193.         changed := false
  194.         for i := 0; i < len(slice); i++ {
  195.             if isUsed(slice[i]) {
  196.                 // cut element i out
  197.                 slice = append(slice[:i], slice[i+1:]...)
  198.                 changed = true
  199.                 break
  200.             }
  201.         }
  202.         if !changed {
  203.             return slice
  204.         }
  205.     }
  206. }
  207.  
  208. func isUsed(word string) bool {
  209.     _, found := used[word]
  210.     return found
  211. }
  212.  
  213. func isDone() bool {
  214.     return len(used) == len(list)
  215. }
  216.  
  217. func saveResult(value string, linkCount int) Result {
  218.     result := Result{}
  219.     result.value = value
  220.     result.linkCount = linkCount
  221.     result.length = len(value)
  222.  
  223.     bestResults = append(bestResults, result)
  224.  
  225.     return result
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement