clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

feat/app-decomposition
puzzletea / games / nonogram / tomography.go
tomography.go 1.7K
1package nonogram23type TomographyDefinition [][]int45type Hints struct {6 rows, cols TomographyDefinition7}89func generateTomography(g grid) Hints {10 height := len(g)11 width := 012 if height > 0 {13 for _, row := range g {14 if len(row) > width {15 width = len(row)16 }17 }18 }1920 rowHints := make([][]int, height)21 for y, row := range g {22 count := 023 var rowHint []int24 for _, tile := range row {25 if tile == filledTile {26 count++27 } else {28 if count > 0 {29 rowHint = append(rowHint, count)30 }31 count = 032 }33 }34 if count > 0 {35 rowHint = append(rowHint, count)36 }37 if len(rowHint) == 0 {38 rowHint = append(rowHint, 0)39 }40 rowHints[y] = rowHint41 }4243 colHints := make([][]int, width)44 for x := range width {45 count := 046 var colHint []int47 for y := range height {48 r := emptyTile49 if x < len(g[y]) {50 r = g[y][x]51 }5253 if r == filledTile {54 count++55 } else {56 if count > 0 {57 colHint = append(colHint, count)58 }59 count = 060 }61 }62 if count > 0 {63 colHint = append(colHint, count)64 }65 if len(colHint) == 0 {66 colHint = append(colHint, 0)67 }68 colHints[x] = colHint69 }7071 return Hints{72 rowHints,73 colHints,74 }75}7677func (t TomographyDefinition) RequiredLen() int {78 var maxLen int79 for _, r := range t {80 if len(r) > maxLen {81 maxLen = len(r)82 }83 }84 return maxLen85}8687func (t TomographyDefinition) equal(o TomographyDefinition) bool {88 if len(t) != len(o) {89 return false90 }91 for i, r := range t {92 if len(r) != len(o[i]) {93 return false94 }95 for j, v := range r {96 if v != o[i][j] {97 return false98 }99 }100 }101 return true102}103104func (t TomographyDefinition) String() string {105 return colHintView(t, t.RequiredLen())106}