From 3d6b9c2f893178b33d962b2ff49b12fbaed4c6e5 Mon Sep 17 00:00:00 2001 From: dean Date: Sun, 19 Sep 2021 00:34:01 -0700 Subject: [PATCH] set default max colors to 2, add output options --- serialize.go | 14 ++++--- solve/cmd/gs-solve/main.go | 42 +++++++++---------- solve/cmd/gs-solve/output.go | 79 ++++++++++++++++++++++++++++++++++++ temp/main.go | 13 +++--- 4 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 solve/cmd/gs-solve/output.go diff --git a/serialize.go b/serialize.go index f7a9615..eb347bb 100644 --- a/serialize.go +++ b/serialize.go @@ -49,13 +49,17 @@ func emptyLine(s string) bool { } func stripEmptyLines(lines []string) []string { - if emptyLine(lines[0]) { - lines = lines[1:] + if len(lines) == 0 { + return nil } - if emptyLine(lines[len(lines)-1]) { - lines = lines[:len(lines)-1] + + restOfLines := stripEmptyLines(lines[1:]) + + if emptyLine(lines[0]) { + return restOfLines } - return lines + + return append(lines[:1], restOfLines...) } // MakeGridFromString takes a string and converts it into a Grid. diff --git a/solve/cmd/gs-solve/main.go b/solve/cmd/gs-solve/main.go index 53da114..db3413e 100644 --- a/solve/cmd/gs-solve/main.go +++ b/solve/cmd/gs-solve/main.go @@ -12,20 +12,21 @@ import ( ) const ( - outputString = "lines" - outputJSON = "json" + outputLines = "lines" + outputJSON = "json" + outputEmoji = "emoji" ) var ( - helpFlag = getopt.BoolLong("help", 'h', "display help") - maxColors = getopt.IntLong("maxcolors", 'm', 0, "the total number of colors available for this level", "2") - solveTiles = getopt.ListLong("tiles", 't', "solve specific tiles. a comma-separated list of space-separated coordinates") - solveGoals = getopt.BoolLong("goals", 'g', "solve all goal tiles") - solveCrowns = getopt.BoolLong("crowns", 'c', "solve all crown tiles") - solveDots = getopt.BoolLong("dots", 'd', "solve all dot tiles") - solveJoins = getopt.BoolLong("joins", 'j', "solve all join tiles") - solveAll = getopt.BoolLong("all", 'a', "solve all tiles") - jsonOutput = getopt.EnumLong("format", 'f', []string{outputString, outputJSON}, "", "output format (lines or json)") + helpFlag = getopt.BoolLong("help", 'h', "display help") + maxColors = getopt.IntLong("maxcolors", 'm', 2, "the total number of colors available for this level", "2") + solveTiles = getopt.ListLong("tiles", 't', "solve specific tiles. a comma-separated list of space-separated coordinates") + solveGoals = getopt.BoolLong("goals", 'g', "solve all goal tiles") + solveCrowns = getopt.BoolLong("crowns", 'c', "solve all crown tiles") + solveDots = getopt.BoolLong("dots", 'd', "solve all dot tiles") + solveJoins = getopt.BoolLong("joins", 'j', "solve all join tiles") + solveAll = getopt.BoolLong("all", 'a', "solve all tiles") + outputFormat = getopt.EnumLong("format", 'f', []string{outputLines, outputJSON, outputEmoji}, outputLines, "output format (lines/json/blocks)") ) func solutionsFromFlags(solver solve.GridSolver) <-chan gridspech.TileSet { @@ -118,15 +119,14 @@ func main() { solver := solve.NewGridSolver(gridspech.MakeGridFromString(level, *maxColors)) solutions := solutionsFromFlags(solver) - - first := true - for solution := range solutions { - if !first { - fmt.Println() - } - - newGrid := solver.Grid.Clone() - newGrid.ApplyTileSet(solution) - fmt.Println(newGrid) + switch *outputFormat { + case outputLines: + printSolutionAsLines(solver, solutions) + case outputJSON: + printSolutionAsJSON(solver, solutions) + case outputEmoji: + printSolutionAsBlocks(solver, solutions) + default: + panic("invalid output format: " + *outputFormat) } } diff --git a/solve/cmd/gs-solve/output.go b/solve/cmd/gs-solve/output.go new file mode 100644 index 0000000..3077cbb --- /dev/null +++ b/solve/cmd/gs-solve/output.go @@ -0,0 +1,79 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "os" + + "github.com/deanveloper/gridspech-go" + "github.com/deanveloper/gridspech-go/solve" +) + +func printSolutionAsLines(solver solve.GridSolver, solutions <-chan gridspech.TileSet) { + first := true + for solution := range solutions { + if !first { + fmt.Println() + } + first = false + + newGrid := solver.Grid.Clone() + newGrid.ApplyTileSet(solution) + fmt.Println(newGrid) + } +} + +func printSolutionAsJSON(solver solve.GridSolver, solutions <-chan gridspech.TileSet) { + var solutionsArray [][][]gridspech.Tile + for solution := range solutions { + newGrid := solver.Grid.Clone() + newGrid.ApplyTileSet(solution) + solutionsArray = append(solutionsArray, newGrid.Tiles) + } + json.NewEncoder(os.Stdout).Encode(solutionsArray) +} + +func printTileSetAsEmoji(w io.Writer, width, height int, solution map[gridspech.TileCoord]gridspech.Tile) { + emojis := []string{ + "⬛", + "🟥", + "🟨", + "🟦", + "🟩", + "🟪", + "🟫", + "⬜", + "🟧", + } + for y := height - 1; y >= 0; y-- { + for x := 0; x < width; x++ { + tile, ok := solution[gridspech.TileCoord{X: x, Y: y}] + if ok { + fmt.Fprint(w, emojis[tile.Data.Color]) + } else { + fmt.Fprint(w, emojis[0]) + } + } + fmt.Fprintln(w) + } +} + +func printSolutionAsBlocks(solver solve.GridSolver, solutions <-chan gridspech.TileSet) { + first := true + + width, height := solver.Grid.Width(), solver.Grid.Height() + for solution := range solutions { + if !first { + fmt.Println() + } + first = false + + solutionMap := make(map[gridspech.TileCoord]gridspech.Tile) + for tile := range solution.Iter() { + solutionMap[tile.Coord] = tile + } + + printTileSetAsEmoji(os.Stdout, width, height, solutionMap) + } +} diff --git a/temp/main.go b/temp/main.go index 6e2e2f7..27928f7 100644 --- a/temp/main.go +++ b/temp/main.go @@ -44,12 +44,13 @@ func tryE8() { func tryTest() { const lvl = ` - 0 0 0 0 0 0 - 0 0e 0k 0 0 0 - 0 0 0k 0 0 0 - 0 0 0k 0 0 0 - 0 0 0k 0e 0 0 - 0j1 0e 0 0 0j1 0e + _ 0 0 0 0/ 0 0 + 0 0 0 0 0 0 0 + 0 _ 0 0e 0 0 0e + 0/ 0 0 0k 0 0 1/ + 0e 0 0 0e 0 _ 0 + 0 0 0 0 0 0 0 + 0 0 1/ 0 0 0 _ ` grid := gridspech.MakeGridFromString(lvl, 2) ch := solve.NewGridSolver(grid).SolveAllTiles()