Skip to content

Instantly share code, notes, and snippets.

@lacolaco
Created August 13, 2014 03:53
Show Gist options
  • Save lacolaco/f774b836abd6acfa17bf to your computer and use it in GitHub Desktop.
Save lacolaco/f774b836abd6acfa17bf to your computer and use it in GitHub Desktop.
bbop in golang
package la0c
import (
"math/rand"
"strconv"
"strings"
)
const (
ANSWER = "ビ,ビ,ッ,ド,レ,ッ,ド,・,オ,ペ,レ,ー,ショ,ン"
MATERIAL = "ビ,ビ,ド,レ,ド,オ,ペ,レ,ショ"
)
type bbopResult struct {
Result string `json:"result"`
MatchCount int `json:"match_count"`
}
func TryBBOP(seed string) bbopResult {
answerArray := strings.Split(ANSWER, ",")
baseArray := strings.Split(ANSWER, ",")
materialArray := strings.Split(MATERIAL, ",")
for i, ans := range answerArray {
switch ans {
case "ッ", "・", "ー", "ン":
continue
default:
if i, err := strconv.ParseInt(seed, 10, 64); err == nil {
rand.Seed(i)
}
index := rand.Intn(9)
baseArray[i] = materialArray[index]
}
}
return bbopResult{
Result: strings.Join(baseArray, ""),
MatchCount: CalcMatchCount(baseArray),
}
}
func CalcMatchCount(result []string) int {
answerArray := strings.Split(ANSWER, ",")
count := 0
for i, ans := range answerArray {
switch ans {
case "ッ", "・", "ー", "ン":
continue
default:
if result[i] == ans {
count++
}
}
}
return count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment