diff --git a/2017/day21/main.go b/2017/day21/main.go index 04b07e2..92a3108 100644 --- a/2017/day21/main.go +++ b/2017/day21/main.go @@ -5,10 +5,8 @@ import ( "fmt" "strings" - "github.com/alexchao26/advent-of-code-go/mathutil" - "github.com/alexchao26/advent-of-code-go/algos" - + "github.com/alexchao26/advent-of-code-go/cast" "github.com/alexchao26/advent-of-code-go/util" ) @@ -74,15 +72,7 @@ func parseInput(input string) map[string][][]string { } return str } - mirrorImageGrid := func(grid [][]string) (flipped [][]string) { - for i := range grid { - flipped = append(flipped, []string{}) - for j := len(grid[i]) - 1; j >= 0; j-- { - flipped[i] = append(flipped[i], grid[i][j]) - } - } - return flipped - } + rules := map[string][][]string{} for _, line := range strings.Split(input, "\n") { parts := strings.Split(line, " => ") @@ -92,7 +82,7 @@ func parseInput(input string) map[string][][]string { for i := 0; i < 4; i++ { keyGrid = algos.RotateStringGrid(keyGrid) rules[stringifyGrid(keyGrid)] = resultGrid - rules[stringifyGrid((mirrorImageGrid(keyGrid)))] = resultGrid + rules[stringifyGrid((algos.MirrorStringGrid(keyGrid)))] = resultGrid } } return rules @@ -108,7 +98,7 @@ func tick(grid [][]string, rules map[string][][]string) [][]string { } else if len(grid)%3 == 0 { edgeSize = 3 } else { - panic("grid is not evenly divisible by 2 or 3, got " + mathutil.IntToStr(len(grid))) + panic("grid is not evenly divisible by 2 or 3, got " + cast.ToString(len(grid))) } // iterate over like a sudoku grid, r and c iterate over the top left corner diff --git a/2020/day20/main.go b/2020/day20/main.go index b2622c9..088dc76 100644 --- a/2020/day20/main.go +++ b/2020/day20/main.go @@ -70,7 +70,7 @@ func part2(input string) int { // get the coordinates of all monsters by iterating over all possible // orientations of the image var monsterCoords [][2]int - for _, opt := range generateGridOrientations(image) { + for _, opt := range algos.AllGridOrientations(image) { monsterCoords = findMonsterCoords(opt) // assuming there's only one orientation of image with valid monsters if len(monsterCoords) > 0 { @@ -121,19 +121,6 @@ func parseTilesFromInput(input string) []*tile { return ans } -func generateGridOrientations(grid [][]string) [][][]string { - var options [][][]string - for i := 0; i < 2; i++ { - for j := 0; j < 4; j++ { - options = append(options, grid) - grid = algos.RotateStringGrid(grid) - } - grid = algos.MirrorStringGrid(grid) - } - // note: there will likely be duplicates in there... but that's fine... - return options -} - func backtrackAssemble(tiles []*tile, assembledTiles [][]*tile, usedIndices map[int]bool) [][]*tile { // pray it's a square... edgeSize := int(math.Sqrt(float64(len(tiles)))) @@ -153,7 +140,7 @@ func backtrackAssemble(tiles []*tile, assembledTiles [][]*tile, usedIndices map[ for i, t := range tiles { if !usedIndices[i] { // iterate over the OPTIONS for a particular tile, i.e. all 8 images of it... - for _, opt := range generateGridOrientations(t.contents) { + for _, opt := range algos.AllGridOrientations(t.contents) { // check if setting this tile is okay with (if applicable) tiles above // and to the left if row != 0 { // check above diff --git a/algos/grid-orientations.go b/algos/grid-orientations.go new file mode 100644 index 0000000..0f2db0c --- /dev/null +++ b/algos/grid-orientations.go @@ -0,0 +1,17 @@ +package algos + +// AllGridOrientations returns the 8 possible orientations of a given grid +// i.e. rotated 4 times and the mirror image of all of those +func AllGridOrientations(grid [][]string) [][][]string { + orientations := [][][]string{grid} + // add the 3 other rotations + for i := 0; i < 3; i++ { + orientations = append(orientations, RotateStringGrid(orientations[len(orientations)-1])) + } + // then add the mirror images of all 4 rotations + for i := 0; i < 4; i++ { + orientations = append(orientations, MirrorStringGrid(orientations[i])) + } + + return orientations +}