From e49ef85eba98eaf6d4c1c6754a14db3bb70d9692 Mon Sep 17 00:00:00 2001 From: Karl Spickermann Date: Sun, 20 Dec 2020 21:52:13 +0100 Subject: [PATCH] Day20 Part2 WIP (2) --- .idea/workspace.xml | 16 +++---- day20/day20.go | 103 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 31 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4da9c0e..75c643d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -324,26 +324,26 @@ - + - - + + - - + + - - + + - + diff --git a/day20/day20.go b/day20/day20.go index c800869..658236a 100644 --- a/day20/day20.go +++ b/day20/day20.go @@ -3,6 +3,7 @@ package main import ( "AoC2020/helper" "fmt" + "math" "os" "regexp" "strconv" @@ -46,45 +47,75 @@ func main() { product *= key } fmt.Println(product) + fmt.Println(tilesWithNeighbours[1951]) var picture = make(map[[2]int]int) - picture = getPictureStep(1951,tilesWithNeighbours,[]int{},picture,[2]int{0,0}) - fmt.Println(picture) + picture = getPictureStep(1951,tilesWithNeighbours,[]int{},picture) } func getPicture(corners map[int]tile, allTiles map[int]tile) map[[2]int]int { var picture = make(map[[2]int]int) //len := math.Sqrt(float64(len(allTiles))) - fmt.Printf("HI %v \n", getSomeTile(corners)) currentTileId := 1951 currentTile := corners[currentTileId] picture[[2]int{0,0}] = currentTileId - fmt.Println(currentTile.neighbourID) - fmt.Println(currentTile.edges) printPicture(currentTile.picture) return picture } -func getPictureStep(currentTile int,allTiles map[int]tile,visitedTiles []int, picture map[[2]int]int,position [2]int) map[[2]int]int{ - for len(visitedTiles) < len(allTiles){ +func getPictureStep(currentTile int,allTiles map[int]tile,visitedTiles []int, picture map[[2]int]int) map[[2]int]int{ + lenght := int(math.Sqrt(float64(len(allTiles)))) + position := [2]int{0,lenght} + for allTiles[currentTile].neighbourID[1] != 0 || allTiles[currentTile].neighbourID[2] != 0{ + allTiles[currentTile] = rotate(allTiles[currentTile]) + fmt.Println(allTiles[currentTile]) + } + fmt.Println(allTiles[currentTile]) + for len(visitedTiles) < 2{ picture[position] = currentTile visitedTiles = append(visitedTiles,currentTile) - nextTile := 0 - for i, val := range allTiles[currentTile].neighbourID { - if val != 0 && !contains(val,visitedTiles){ - nextTile = val - switch i { - case 0: position = [2]int{position[0],position[1]+1} - case 1: position = [2]int{position[0]+1,position[1]} - case 2: position = [2]int{position[0],position[1]-1} - case 3: position = [2]int{position[0]-1,position[1]} - } - } - } - currentTile = nextTile + nextPosition, _ := getNextPosition(position,lenght,picture) + //nextTileId, _ := getNextTile(allTiles[currentTile].edges[edge],allTiles,currentTile) + position = nextPosition } return picture } +func getNextPosition(position [2]int, length int, picture map[[2]int]int) ([2]int, int) { + if (position[1]+1) < length && picture[[2]int{position[0],position[1]+1}] == 0 { + return [2]int{position[0],position[1]+1}, 0 + } + if (position[0]-1) > 0 && picture[[2]int{position[0]-1,position[1]}] == 0 { + return [2]int{position[0]-1,position[1]}, 3 + } + if (position[1]-1) > 0 && picture[[2]int{position[0],position[1]-1}] == 0 { + return [2]int{position[0],position[1]-1}, 2 + } + if (position[0]+1) < 0 && picture[[2]int{position[0]+1,position[1]}] == 0 { + return [2]int{position[0]+1,position[1]}, 1 + } + return position, -1 +} + +func getNextTile(edge string, allTiles map[int]tile, currentTile int) (int, tile){ + var newTile tile + for id, tile := range allTiles{ + if id != currentTile { + if stringContains(edge, tile.edges[:]) { + return id, tile + } + newTile = flip(tile, 0) + if stringContains(edge, newTile.edges[:]) { + return id, tile + } + newTile = flip(tile, 1) + if stringContains(edge, newTile.edges[:]) { + return id, tile + } + } + } + return 0,tile{} +} + func contains(elem int, array []int) bool{ for _, val := range array{ if val == elem{ @@ -94,6 +125,15 @@ func contains(elem int, array []int) bool{ return false } +func stringContains(elem string, array []string) bool{ + for _, val := range array{ + if val == elem{ + return true + } + } + return false +} + func getSomeTile(m map[int]tile) int { for k := range m { return k @@ -124,6 +164,7 @@ func rotate(tile tile) tile{ for i,row := range newPicture { tile.picture[i] = string(row[:]) } + tile = setEdges(tile) return tile } @@ -132,7 +173,7 @@ func rotate(tile tile) tile{ func flip(tile tile, direction int) tile{ var newPicture []string if direction == 0{ - for i := len(tile.picture); i >= 0; i--{ + for i := len(tile.picture)-1; i >= 0; i--{ newPicture = append(newPicture,tile.picture[i]) } } @@ -141,7 +182,10 @@ func flip(tile tile, direction int) tile{ newPicture = append(newPicture,reverse(val)) } } - printPicture(newPicture) + newEdges := getAllPossibleEdgesForFlippedTile(tile,direction) + for i:= 0; i < len(tile.edges); i++{ + tile.edges[i] = newEdges[i] + } tile.picture = newPicture return tile } @@ -166,7 +210,20 @@ func getTile(input string) (int, tile) { } r, _ := regexp.Compile("[0-9]{4}") number, _ := strconv.Atoi(r.FindString(tilesRow[0])) - return number, tile{[4]int{},edges, tilesRow[1:]} + tile := tile{[4]int{},edges, tilesRow[1:]} + return number, setEdges(tile) +} + +func setEdges(tile tile)tile{ + var newEdges [4]string + newEdges[0] = tile.picture[0] + newEdges[2] = tile.picture[len(tile.picture)-1] + for _, val := range tile.picture { + newEdges[3] = newEdges[3]+val[0:1] + newEdges[1] = newEdges[1]+val[len(val)-1:] + } + tile.edges = newEdges + return tile } func getAllPossibleEdgesForTile(tile tile, direction int) []string{