Skip to content

Commit

Permalink
set default max colors to 2, add output options
Browse files Browse the repository at this point in the history
  • Loading branch information
deanveloper committed Sep 19, 2021
1 parent 51f2470 commit 3d6b9c2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 32 deletions.
14 changes: 9 additions & 5 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 21 additions & 21 deletions solve/cmd/gs-solve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
79 changes: 79 additions & 0 deletions solve/cmd/gs-solve/output.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 7 additions & 6 deletions temp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3d6b9c2

Please # to comment.