72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package main
|
|
|
|
import "flag"
|
|
import "fmt"
|
|
import "log"
|
|
import "math/rand"
|
|
import "os"
|
|
import "strings"
|
|
import "time"
|
|
|
|
import "github.com/firestuff/subcoding/asm"
|
|
import "github.com/firestuff/subcoding/grow"
|
|
|
|
func main() {
|
|
defPath := flag.String("def-path", "", "path to definition YAML file")
|
|
flag.Parse()
|
|
|
|
if *defPath == "" {
|
|
log.Fatal("Please specify --def-path")
|
|
}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
defFile, err := os.Open(*defPath)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
def, err := grow.NewDefinition(defFile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
statusChan := make(chan grow.Status)
|
|
|
|
go func() {
|
|
_, err = def.Grow(statusChan)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
for {
|
|
status, ok := <-statusChan
|
|
|
|
if !ok {
|
|
break
|
|
}
|
|
|
|
if status.BestProgram == nil {
|
|
continue
|
|
}
|
|
|
|
src, err := asm.Disassemble(status.BestProgram)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Printf("New best score [%s] after %d attempts:\n%s", scoreString(status.BestScores), status.Attempts, src)
|
|
}
|
|
}
|
|
|
|
func scoreString(scores []*grow.Score) string {
|
|
strs := []string{}
|
|
|
|
for _, score := range scores {
|
|
strs = append(strs, fmt.Sprintf("%d / %d", score.Current, score.Total))
|
|
}
|
|
|
|
return strings.Join(strs, ", ")
|
|
}
|