From 515034a64e3ea382e05cc8461e9e6fc756c943af Mon Sep 17 00:00:00 2001 From: Sacha Ligthert Date: Tue, 28 Jan 2025 01:04:38 +0100 Subject: [PATCH] Add the ability to select type of output. --- controller/types.go | 2 ++ export/flat.go | 14 ++++++++++++++ export/human-readable.go | 2 +- flags/parse.go | 10 ++++++++++ main.go | 8 +++++++- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 export/flat.go diff --git a/controller/types.go b/controller/types.go index af9e509..74df29c 100644 --- a/controller/types.go +++ b/controller/types.go @@ -30,4 +30,6 @@ type Controller struct { Split int // Which part of the search should the solver focus on. Part int + // Type of output requested + Output string } diff --git a/export/flat.go b/export/flat.go new file mode 100644 index 0000000..3ca97af --- /dev/null +++ b/export/flat.go @@ -0,0 +1,14 @@ +package export + +import ( + "fmt" + "log" +) + +// Print solutions into a human friendly format for in the console. +func (export *Export) PrintFlatSolutions() { + for solutionIndex, solution := range export.Controller.Solutions { + log.Printf("\nSolution #%d:", solutionIndex+1) + fmt.Println(solution) + } +} diff --git a/export/human-readable.go b/export/human-readable.go index 3f4ac0f..49228eb 100644 --- a/export/human-readable.go +++ b/export/human-readable.go @@ -6,7 +6,7 @@ import ( ) // Print solutions into a human friendly format for in the console. -func (export *Export) PrintSolutions() { +func (export *Export) PrintHumanSolutions() { for solutionIndex, solution := range export.Controller.Solutions { log.Printf("\nSolution #%d:", solutionIndex+1) //fmt.Println(solution) diff --git a/flags/parse.go b/flags/parse.go index d5e38e2..198f03e 100644 --- a/flags/parse.go +++ b/flags/parse.go @@ -6,6 +6,7 @@ import ( "log" "os" "runtime" + "strings" ) // Parse command-line parameters, test input, store them in the Controller. @@ -24,6 +25,7 @@ func (flags *Flags) ParseFlags() { flag.IntVar(&flags.Controller.NumCPUs, "numcpu", runtime.NumCPU(), "Number of CPU cores to assign to this task.") flag.IntVar(&flags.Controller.Split, "split", 1, "Split the tasks in n parts. This depends on the availability of the first row.") flag.IntVar(&flags.Controller.Part, "part", 1, "Process part x in n parts. Cannot be lower than 1, or higher than specified in split.") + flag.StringVar(&flags.Controller.Output, "output", "human", "Type of output. 'human' for human readable. 'flat' for flat as stored in memory output.") // Parse the flags flag.Parse() @@ -72,6 +74,14 @@ func (flags *Flags) ParseFlags() { os.Exit(1) } + // Process output selection + flags.Controller.Output = strings.ToLower(flags.Controller.Output) + if flags.Controller.Output != "human" && flags.Controller.Output != "flat" { + log.Printf("ERROR: Invalid output, can only be 'human' or 'flat'.\n") + flags.printUsage() + os.Exit(1) + } + } // Validate if a row is properly set. diff --git a/main.go b/main.go index 185e3e5..96b61b7 100644 --- a/main.go +++ b/main.go @@ -46,5 +46,11 @@ func main() { solver.Tracker() // Print the valid solutions - export.PrintSolutions() + switch controller.Output { + case "human": + export.PrintHumanSolutions() + case "flat": + export.PrintFlatSolutions() + } + }