github.com/joshprzybyszewski/masyu@v0.0.0-20230508015604-f31a025f6e7e/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "runtime" 7 "time" 8 9 "github.com/joshprzybyszewski/masyu/fetch" 10 "github.com/joshprzybyszewski/masyu/model" 11 "github.com/joshprzybyszewski/masyu/profile" 12 "github.com/joshprzybyszewski/masyu/results" 13 "github.com/joshprzybyszewski/masyu/solve" 14 ) 15 16 var ( 17 updateResults = flag.Bool("results", false, "if set, then it will print the custom benchmark results") 18 19 puzzID = flag.String("puzzID", "", "if set, then this will run a specific puzzle") 20 21 iterStart = flag.Int("start", int(model.MinIterator), "if set, this will override the iterators starting value") 22 iterFinish = flag.Int("finish", int(model.MaxIterator), "if set, this will override the iterators final value") 23 numIterations = flag.Int("numIterations", 1, "set this value to run through the puzzles many times") 24 25 fetchNewPuzzles = flag.Bool("refresh", true, "if set, then it will fetch new puzzles") 26 27 shouldProfile = flag.Bool("profile", false, "if set, will produce a profile output") 28 ) 29 30 func main() { 31 flag.Parse() 32 33 if *updateResults { 34 results.Update() 35 return 36 } 37 38 if *shouldProfile { 39 defer profile.Start()() 40 } 41 42 if *puzzID != `` { 43 _ = runPuzzleID( 44 model.Iterator(*iterStart), 45 *puzzID, 46 ) 47 return 48 } 49 50 for i := 0; i < *numIterations; i++ { 51 for iter := model.Iterator(*iterStart); iter <= model.Iterator(*iterFinish); iter++ { 52 for numGCs := 0; numGCs < 5; numGCs++ { 53 time.Sleep(100 * time.Millisecond) 54 runtime.GC() 55 } 56 57 err := compete(iter) 58 if err != nil { 59 fmt.Printf("Error: %+v\n", err) 60 // panic(err) 61 } 62 time.Sleep(time.Second) 63 } 64 } 65 } 66 67 func compete(iter model.Iterator) error { 68 69 fmt.Printf("Starting %s\n\t%s\n\n\n", iter, time.Now()) 70 input, err := fetch.Puzzle(iter) 71 if *fetchNewPuzzles { 72 input, err = fetch.GetNewPuzzle(iter) 73 } 74 fmt.Printf("Iter: %q, PuzzleID: %q, Task: %q\n", iter, input.ID, input.Task()) 75 76 if err != nil { 77 return err 78 } 79 80 ns := input.ToNodes() 81 82 t0 := time.Now() 83 sol, err := solve.FromNodes( 84 iter.GetSize(), 85 ns, 86 ) 87 defer func(dur time.Duration) { 88 fmt.Printf("Input: %s\n", input) 89 fmt.Printf("Solution:\n%s\n", sol.Pretty(ns)) 90 fmt.Printf("Duration: %s\n\n\n", dur) 91 }(time.Since(t0)) 92 93 if err != nil { 94 _ = fetch.StorePuzzle(&input) 95 return err 96 } 97 98 return fetch.Submit( 99 &input, 100 &sol, 101 ) 102 } 103 104 func runPuzzleID( 105 iter model.Iterator, 106 id string, 107 ) error { 108 fmt.Printf("Starting %s\n\t%s\n\n\n", iter, time.Now()) 109 input, err := fetch.GetPuzzleID(iter, id) 110 if err != nil { 111 return err 112 } 113 114 ns := input.ToNodes() 115 116 t0 := time.Now() 117 sol, err := solve.FromNodes( 118 iter.GetSize(), 119 ns, 120 ) 121 defer func(dur time.Duration) { 122 fmt.Printf("Input: %s\n", input) 123 fmt.Printf("Solution:\n%s\n", sol.Pretty(ns)) 124 fmt.Printf("Duration: %s\n\n\n", dur) 125 }(time.Since(t0)) 126 127 if err != nil { 128 return err 129 } 130 131 return fetch.Submit( 132 &input, 133 &sol, 134 ) 135 }