Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bytes"
- "fmt"
- "math"
- "math/rand"
- "time"
- )
- type DecisionNode struct {
- position int
- totalPosition int
- word string
- result string
- }
- type Result struct {
- value string
- linkCount int
- length int
- }
- 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"}
- var decisions DecisionNode
- var used map[string]bool
- var bestResults []Result
- var timer int
- var progressTimer int
- var progressTicksSinceBetterFound int
- var timerInitial int = 50000
- var bestResultLinkCount int = 0
- var bestResultLength int = math.MaxInt32
- func main() {
- rand.Seed(time.Now().UTC().UnixNano())
- timer = timerInitial
- run()
- }
- func run() {
- initialDecision := &decisions
- for {
- // Clear the map.
- used = make(map[string]bool)
- initialDecision.position = 0
- initialDecision.totalPosition = 0
- i := rand.Intn(len(list))
- initialDecision.word = list[i]
- initialDecision.result = list[i]
- used[list[i]] = true
- bruteForce(initialDecision, 0)
- }
- fmt.Println("Best Results: ", len(bestResults))
- fmt.Println(bestResults)
- }
- func bruteForce(decision *DecisionNode, linkCount int) bool {
- progressTimer++
- if progressTimer == 10000 {
- fmt.Println("Progress: ", decision.result, timer)
- progressTimer = 0
- progressTicksSinceBetterFound++
- }
- timer--
- if timer == 0 {
- fmt.Println("Reseeding, was on", decision.result, timer)
- timer = timerInitial
- return true
- }
- linkCount++
- length := len(decision.result)
- if bestResultLinkCount < linkCount {
- // Found a better link count
- bestResults = nil
- bestResultLinkCount = linkCount
- bestResultLength = length
- timer += 2000 * progressTicksSinceBetterFound
- fmt.Println("Found better link count: ", saveResult(decision.result, linkCount), timer)
- progressTicksSinceBetterFound = 0
- } else if bestResultLinkCount == linkCount {
- if bestResultLength >= length {
- // Found at least as good of a length
- better := false
- if bestResultLength > length {
- // This length is better - clear the old results
- better = true
- bestResults = nil
- }
- bestResultLength = length
- result := saveResult(decision.result, linkCount)
- timer += 2000 * progressTicksSinceBetterFound
- if better {
- fmt.Println("Found better length: ", result, timer)
- }
- progressTicksSinceBetterFound = 0
- }
- }
- var currentList []string = make([]string, len(list))
- copy(currentList, list)
- currentList = removeUsedWords(currentList)
- positionsPerm := rand.Perm(len(decision.word))
- for i := 0; i < len(positionsPerm); i++ {
- decision.position = positionsPerm[i]
- indexInNextWord := 0
- skipPosition := false
- var newList []string = make([]string, len(currentList))
- copy(newList, currentList)
- for j := decision.position; j < len(decision.word); j++ {
- newSlice := getByLetter(newList, decision.word[j], indexInNextWord)
- newSlice = removeUsedWords(newSlice)
- if len(newSlice) > 0 {
- newList = newSlice
- } else {
- // Couldn't find any word that fit starting at decision.position
- indexInNextWord = 0
- skipPosition = true
- break
- }
- indexInNextWord++
- }
- if !skipPosition {
- newWordPerm := rand.Perm(len(newList))
- for j := 0; j < len(newWordPerm); j++ {
- word := newList[newWordPerm[j]]
- used[word] = true
- nextDecision := DecisionNode{}
- nextDecision.word = word
- nextDecision.totalPosition = decision.totalPosition + decision.position
- nextDecision.result = concat(decision.result, nextDecision.word, nextDecision.totalPosition)
- if bruteForce(&nextDecision, linkCount) {
- return true
- }
- delete(used, word)
- }
- }
- decision.position++
- if decision.position == len(decision.word) {
- break
- }
- }
- return false
- }
- func concat(s1 string, s2 string, positionInS1 int) string {
- var buffer bytes.Buffer
- buffer.WriteString(s1[:positionInS1])
- buffer.WriteString(s2)
- return buffer.String()
- }
- func getByLetter(slice []string, letter uint8, index int) []string {
- first := -1
- for i := 0; i < len(slice); i++ {
- if len(slice[i]) <= index {
- continue
- }
- if slice[i][index] == letter {
- first = i
- break
- }
- }
- if first == -1 {
- return slice[:0]
- }
- last := first + 1
- for i := first + 1; i < len(slice); i++ {
- if len(slice[i]) <= index {
- continue
- }
- if slice[i][index] != letter {
- last = i
- break
- }
- }
- return slice[first:last]
- }
- func removeUsedWords(slice []string) []string {
- for {
- changed := false
- for i := 0; i < len(slice); i++ {
- if isUsed(slice[i]) {
- // cut element i out
- slice = append(slice[:i], slice[i+1:]...)
- changed = true
- break
- }
- }
- if !changed {
- return slice
- }
- }
- }
- func isUsed(word string) bool {
- _, found := used[word]
- return found
- }
- func isDone() bool {
- return len(used) == len(list)
- }
- func saveResult(value string, linkCount int) Result {
- result := Result{}
- result.value = value
- result.linkCount = linkCount
- result.length = len(value)
- bestResults = append(bestResults, result)
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement