mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-06 20:30:12 +02:00
moved grid orientation generation into algos package
This commit is contained in:
+4
-14
@@ -5,10 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alexchao26/advent-of-code-go/mathutil"
|
|
||||||
|
|
||||||
"github.com/alexchao26/advent-of-code-go/algos"
|
"github.com/alexchao26/advent-of-code-go/algos"
|
||||||
|
"github.com/alexchao26/advent-of-code-go/cast"
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -74,15 +72,7 @@ func parseInput(input string) map[string][][]string {
|
|||||||
}
|
}
|
||||||
return str
|
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{}
|
rules := map[string][][]string{}
|
||||||
for _, line := range strings.Split(input, "\n") {
|
for _, line := range strings.Split(input, "\n") {
|
||||||
parts := strings.Split(line, " => ")
|
parts := strings.Split(line, " => ")
|
||||||
@@ -92,7 +82,7 @@ func parseInput(input string) map[string][][]string {
|
|||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
keyGrid = algos.RotateStringGrid(keyGrid)
|
keyGrid = algos.RotateStringGrid(keyGrid)
|
||||||
rules[stringifyGrid(keyGrid)] = resultGrid
|
rules[stringifyGrid(keyGrid)] = resultGrid
|
||||||
rules[stringifyGrid((mirrorImageGrid(keyGrid)))] = resultGrid
|
rules[stringifyGrid((algos.MirrorStringGrid(keyGrid)))] = resultGrid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rules
|
return rules
|
||||||
@@ -108,7 +98,7 @@ func tick(grid [][]string, rules map[string][][]string) [][]string {
|
|||||||
} else if len(grid)%3 == 0 {
|
} else if len(grid)%3 == 0 {
|
||||||
edgeSize = 3
|
edgeSize = 3
|
||||||
} else {
|
} 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
|
// iterate over like a sudoku grid, r and c iterate over the top left corner
|
||||||
|
|||||||
+2
-15
@@ -70,7 +70,7 @@ func part2(input string) int {
|
|||||||
// get the coordinates of all monsters by iterating over all possible
|
// get the coordinates of all monsters by iterating over all possible
|
||||||
// orientations of the image
|
// orientations of the image
|
||||||
var monsterCoords [][2]int
|
var monsterCoords [][2]int
|
||||||
for _, opt := range generateGridOrientations(image) {
|
for _, opt := range algos.AllGridOrientations(image) {
|
||||||
monsterCoords = findMonsterCoords(opt)
|
monsterCoords = findMonsterCoords(opt)
|
||||||
// assuming there's only one orientation of image with valid monsters
|
// assuming there's only one orientation of image with valid monsters
|
||||||
if len(monsterCoords) > 0 {
|
if len(monsterCoords) > 0 {
|
||||||
@@ -121,19 +121,6 @@ func parseTilesFromInput(input string) []*tile {
|
|||||||
return ans
|
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 {
|
func backtrackAssemble(tiles []*tile, assembledTiles [][]*tile, usedIndices map[int]bool) [][]*tile {
|
||||||
// pray it's a square...
|
// pray it's a square...
|
||||||
edgeSize := int(math.Sqrt(float64(len(tiles))))
|
edgeSize := int(math.Sqrt(float64(len(tiles))))
|
||||||
@@ -153,7 +140,7 @@ func backtrackAssemble(tiles []*tile, assembledTiles [][]*tile, usedIndices map[
|
|||||||
for i, t := range tiles {
|
for i, t := range tiles {
|
||||||
if !usedIndices[i] {
|
if !usedIndices[i] {
|
||||||
// iterate over the OPTIONS for a particular tile, i.e. all 8 images of it...
|
// 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
|
// check if setting this tile is okay with (if applicable) tiles above
|
||||||
// and to the left
|
// and to the left
|
||||||
if row != 0 { // check above
|
if row != 0 { // check above
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user