add halp package for printing infinite grids, which seems to be quite common. generics may be useful here oneday...

This commit is contained in:
alexchao26
2021-12-30 20:03:25 -05:00
parent a8ffba20f7
commit 2a3bffd659
4 changed files with 71 additions and 52 deletions
+3 -24
View File
@@ -95,7 +95,8 @@ func enhanceImg(img map[[2]int]string, alg []string, infiniteSpaceStaysOff, infi
}
// fmt.Println("BEFORE")
// fmt.Println(imgString(img))
// debugging helper to print an infinite grid for
// halp.PrintInfiniteGridStrings(img, ".")
// now only need to check within firstRow - 2 through lastRow + 2 (same for cols)
// because flickers will kill that third row/col out
@@ -109,7 +110,7 @@ func enhanceImg(img map[[2]int]string, alg []string, infiniteSpaceStaysOff, infi
}
// fmt.Println("AFTER")
// fmt.Println(imgString(next))
// halp.PrintInfiniteGridStrings(next, ".")
return next
}
@@ -155,25 +156,3 @@ func parseInput(input string) (alg []string, img map[[2]int]string) {
return alg, img
}
// printing for debugging
// TODO pull into algos package for easier debugging. make a printing package?
func imgString(img map[[2]int]string) string {
// get bounds
var firstRow, lastRow, firstCol, lastCol int
for coord := range img {
firstRow = mathy.MinInt(firstRow, coord[0])
lastRow = mathy.MaxInt(lastRow, coord[0])
firstCol = mathy.MinInt(firstCol, coord[1])
lastCol = mathy.MaxInt(lastCol, coord[1])
}
var sb strings.Builder
for r := firstRow; r <= lastRow; r++ {
for c := firstCol; c <= lastCol; c++ {
sb.WriteString(img[[2]int{r, c}])
}
sb.WriteString("\n")
}
return sb.String()
}