mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
add halp package for printing infinite grids, which seems to be quite common. generics may be useful here oneday...
This commit is contained in:
+6
-28
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
"github.com/alexchao26/advent-of-code-go/halp"
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
@@ -91,34 +92,11 @@ func transparentOrigamiDay13(input string, part int) int {
|
||||
|
||||
// printing is a pita but necessary for reading part2
|
||||
if part == 2 {
|
||||
max := 0
|
||||
for c := range coords {
|
||||
if c[0] > max {
|
||||
max = c[0]
|
||||
}
|
||||
if c[1] > max {
|
||||
max = c[1]
|
||||
}
|
||||
}
|
||||
|
||||
grid := make([][]int, max+1)
|
||||
for i := range grid {
|
||||
grid[i] = make([]int, max+1)
|
||||
}
|
||||
for c := range coords {
|
||||
grid[c[1]][c[0]] = 1
|
||||
}
|
||||
for _, row := range grid {
|
||||
str := ""
|
||||
for _, val := range row {
|
||||
if val == 1 {
|
||||
str += "#"
|
||||
} else {
|
||||
str += "."
|
||||
}
|
||||
}
|
||||
fmt.Println(str)
|
||||
}
|
||||
// NOTE: as usual my printing is rotated and mirrored because my mental
|
||||
// mapping of x/y uses rows/cols and ends up different than AOC :/
|
||||
// Maybe next year I'll finally change my mental map... or not :)
|
||||
halp.PrintInfiniteGridBools(coords, "#", ".")
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
+3
-24
@@ -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()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// Package halp is a bunch of helpers for AOC specific debugging like printing
|
||||
// infinite grids (map[[2]int]<T> <- Go generics will be useful here...)
|
||||
package halp
|
||||
@@ -0,0 +1,59 @@
|
||||
package halp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/mathy"
|
||||
)
|
||||
|
||||
// PrintInfiniteGridStrings supports the type map[[2]int]string, determines the
|
||||
// bounds of that infinite grid, and consolidates it into a string AND PRINTS IT
|
||||
// zeroValChar should be one character, and replaces any grid coordinate NOT in
|
||||
// the infiniteGrid
|
||||
func PrintInfiniteGridStrings(infiniteGrid map[[2]int]string, zeroValChar string) {
|
||||
// get bounds
|
||||
var firstRow, lastRow, firstCol, lastCol int
|
||||
for coord := range infiniteGrid {
|
||||
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++ {
|
||||
coord := [2]int{r, c}
|
||||
if val, ok := infiniteGrid[coord]; ok {
|
||||
sb.WriteString(val)
|
||||
} else {
|
||||
sb.WriteString(zeroValChar)
|
||||
}
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
fmt.Println(sb.String())
|
||||
}
|
||||
|
||||
// convertMapBoolsToMapStrings is a helper function for printing map[[2]int]bool
|
||||
// The return map can be passed into PrintInfiniteGridStrings
|
||||
func convertMapBoolsToMapStrings(m map[[2]int]bool, trueChar, falseChar string) map[[2]int]string {
|
||||
converted := map[[2]int]string{}
|
||||
|
||||
for k, v := range m {
|
||||
if v {
|
||||
converted[k] = trueChar
|
||||
} else {
|
||||
converted[k] = falseChar
|
||||
}
|
||||
}
|
||||
|
||||
return converted
|
||||
}
|
||||
|
||||
// PrintInfiniteGridBools supports the type map[[2]int]bool
|
||||
func PrintInfiniteGridBools(m map[[2]int]bool, trueChar, falseChar string) {
|
||||
mapCoordsToStrings := convertMapBoolsToMapStrings(m, trueChar, falseChar)
|
||||
PrintInfiniteGridStrings(mapCoordsToStrings, falseChar)
|
||||
}
|
||||
Reference in New Issue
Block a user