Files
subcoding/grow/cli/main.go

60 lines
993 B
Go
Raw Normal View History

2021-11-20 18:51:19 -10:00
package main
import "flag"
import "log"
2021-11-20 19:32:44 -10:00
import "math/rand"
2021-11-20 18:51:19 -10:00
import "os"
2021-11-20 19:32:44 -10:00
import "time"
2021-11-20 18:51:19 -10:00
2021-11-20 20:02:16 -10:00
import "github.com/firestuff/subcoding/asm"
2021-11-20 19:38:31 -10:00
import "github.com/firestuff/subcoding/grow"
2021-11-20 18:51:19 -10:00
func main() {
defPath := flag.String("def-path", "", "path to definition YAML file")
flag.Parse()
if *defPath == "" {
log.Fatal("Please specify --def-path")
}
2021-11-20 19:32:44 -10:00
rand.Seed(time.Now().UnixNano())
2021-11-20 18:51:19 -10:00
defFile, err := os.Open(*defPath)
if err != nil {
log.Fatal(err)
}
2021-11-20 19:38:31 -10:00
def, err := grow.NewDefinition(defFile)
2021-11-20 18:51:19 -10:00
if err != nil {
log.Fatal(err)
}
2021-11-20 20:02:16 -10:00
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 %d / %d (after %d attempts):\n%s", status.BestScore, status.TargetScore, status.Attempts, src)
2021-11-20 19:38:31 -10:00
}
2021-11-20 18:51:19 -10:00
}