Day20 Part2 WIP

This commit is contained in:
Karl Spickermann 2020-12-20 20:19:05 +01:00
parent 7961849040
commit ce5d071d3c
2 changed files with 185 additions and 58 deletions

View File

@ -11,9 +11,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/day20/day20.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day20/day20Test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/day20/day20.go" beforeDir="false" afterPath="$PROJECT_DIR$/day20/day20.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -178,7 +177,7 @@
<configuration name="go build day20.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day20" />
<parameters value="day20Input.txt" />
<parameters value="day20Test.txt" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/day20/day20.go" />
<package value="AoC2020" />
@ -312,11 +311,11 @@
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1608477052965">
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1608485205303">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="-1472" y="103" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607019292652" />
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608477052965" />
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608485205303" />
<state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -325,26 +324,26 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="739" y="287" key="FileChooserDialogImpl/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607278759015" />
<state width="1172" height="347" key="GridCell.Tab.0.bottom" timestamp="1608480561089">
<state width="1171" height="347" key="GridCell.Tab.0.bottom" timestamp="1608491816624">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
<state width="1172" height="347" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608480561089" />
<state width="1172" height="347" key="GridCell.Tab.0.center" timestamp="1608480561089">
<state width="1171" height="347" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608491816624" />
<state width="1171" height="347" key="GridCell.Tab.0.center" timestamp="1608491816624">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
<state width="1172" height="347" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608480561089" />
<state width="1172" height="347" key="GridCell.Tab.0.left" timestamp="1608480561089">
<state width="1171" height="347" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608491816624" />
<state width="1171" height="347" key="GridCell.Tab.0.left" timestamp="1608491816624">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
<state width="1172" height="347" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608480561089" />
<state width="1172" height="347" key="GridCell.Tab.0.right" timestamp="1608480561089">
<state width="1171" height="347" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608491816624" />
<state width="1171" height="347" key="GridCell.Tab.0.right" timestamp="1608491816624">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
<state width="1172" height="347" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608480561089" />
<state width="1171" height="347" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608491816624" />
<state x="782" y="240" key="RollbackChangesDialog" timestamp="1608474837005">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -353,21 +352,21 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="461" y="165" key="SettingsEditor/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607081830252" />
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1607705394476">
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1608488889272">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607705394476" />
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.SelectionHistoryDialog" timestamp="1608112646663">
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608488889272" />
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.SelectionHistoryDialog" timestamp="1608488889272">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.SelectionHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608112646663" />
<state x="593" y="141" width="585" height="801" key="find.popup" timestamp="1608475300269">
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.SelectionHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608488889272" />
<state x="593" y="141" width="585" height="801" key="find.popup" timestamp="1608483030515">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="593" y="141" width="585" height="801" key="find.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608475300269" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1608045273455">
<state x="593" y="141" width="585" height="801" key="find.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608483030515" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1608484725602">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608045273455" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608484725602" />
</component>
</project>

View File

@ -9,11 +9,12 @@ import (
"strings"
)
var lineBreak = "\n"
var lineBreak = "\r\n"
type tile struct {
neighbourID [4]int
edges [4]string
picture []string
}
func main() {
@ -23,28 +24,138 @@ func main() {
fmt.Println(err)
}
tileInput := strings.Split(input, lineBreak+lineBreak)
fmt.Println(len(tileInput))
tiles := getTiles(tileInput)
corners := findCorners(tiles)
fmt.Println(corners)
product := corners[0]
for _,val := range corners[1:]{
product *= val
var tilesWithNeighbours = make(map[int]tile)
for id,tile := range tiles {
tilesWithNeighbours[id] = getNeighboursForTile(id,tile,tiles)
}
var corners = make(map[int]tile)
for key, tile := range tilesWithNeighbours {
pictureEdges := 0
for _, val := range tile.neighbourID{
if val == 0 {
pictureEdges++
}
}
if pictureEdges == 2 {
corners[key] = tile
}
}
product := 1
for key,_ := range corners {
product *= key
}
fmt.Println(product)
var picture = make(map[[2]int]int)
picture = getPictureStep(1951,tilesWithNeighbours,[]int{},picture,[2]int{0,0})
fmt.Println(picture)
}
func getTiles(input []string) map[int][4]string{
var tiles = make(map[int][4]string)
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){
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
}
return picture
}
func contains(elem int, array []int) 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
}
return 0
}
func printPicture(input[]string){
for _, row := range input {
fmt.Println(row)
}
fmt.Println()
}
func rotate(tile tile) tile{
var newPicture [10][10]rune
for i:= 0; i < len(tile.picture); i++ {
for j:= 0; j < len(tile.picture); j++ {
newPicture[i][j] = rune(tile.picture[len(tile.picture)-j-1][i]);
}
}
var newNeighbourId [4]int
newNeighbourId[0] = tile.neighbourID[3]
for i,val := range tile.neighbourID[:2]{
newNeighbourId[i+1] = val
}
tile.neighbourID = newNeighbourId
for i,row := range newPicture {
tile.picture[i] = string(row[:])
}
return tile
}
//0 horizontal
//1 vertical
func flip(tile tile, direction int) tile{
var newPicture []string
if direction == 0{
for i := len(tile.picture); i >= 0; i--{
newPicture = append(newPicture,tile.picture[i])
}
}
if direction == 1{
for _,val := range tile.picture {
newPicture = append(newPicture,reverse(val))
}
}
printPicture(newPicture)
tile.picture = newPicture
return tile
}
func getTiles(input []string) map[int]tile{
var tiles = make(map[int]tile)
for _, val := range input {
id, edges := getTile(val)
fmt.Printf("ID:%v \n",id )
tiles[id] = edges
id, tile := getTile(val)
tiles[id] = tile
}
return tiles
}
func getTile(input string) (int, [4]string) {
func getTile(input string) (int, tile) {
tilesRow := strings.Split(input, lineBreak)
var edges [4]string
edges[0] = tilesRow[1]
@ -55,42 +166,57 @@ func getTile(input string) (int, [4]string) {
}
r, _ := regexp.Compile("[0-9]{4}")
number, _ := strconv.Atoi(r.FindString(tilesRow[0]))
return number, edges
return number, tile{[4]int{},edges, tilesRow[1:]}
}
func getAllEdges(tiles map[int][4]string, id int) []string{
var edges []string
for key,val := range tiles {
if key != id {
for _, edge := range val {
edges = append(edges, edge)
edges = append(edges, reverse(edge))
}
func getAllPossibleEdgesForTile(tile tile, direction int) []string{
switch direction {
case -1:
var edges [4]string
for i, edge := range tile.edges {
edges[i] = edge
}
return edges[:]
case 0:
return getAllPossibleEdgesForFlippedTile(tile,0)
case 1:
return getAllPossibleEdgesForFlippedTile(tile,1)
}
return edges
return []string{}
}
func findCorners(tiles map[int][4]string) []int{
var cornerIds []int
for key,val := range tiles{
edgesOfOtherTiles := getAllEdges(tiles,key)
fmt.Printf("ID: %v, MatchingEdges: %v \n",key,countMatchingEges(edgesOfOtherTiles,val))
if countMatchingEges(edgesOfOtherTiles,val) <= 2{
cornerIds= append(cornerIds,key)
func getAllPossibleEdgesForFlippedTile(tile tile, direction int) []string{
var edges [4]string
for i:= 0; i < len(tile.edges); i ++ {
if i % 2 == direction {
edges[i] = reverse(tile.edges[i])
}else{
edges[i] = reverse(tile.edges[i])
}
}
return cornerIds
return edges[:]
}
func countMatchingEges(allEdges []string, tileEdges [4]string) int{
count := 0
for _, val := range tileEdges{
if hasMatchingEdge(allEdges,val){
count ++
func getNeighboursForTile(choosenTile int, tile tile, allTiles map[int]tile) tile{
var idToPossibleEdges = make(map[int][]string)
for id,tile := range allTiles {
idToPossibleEdges[id] = getAllPossibleEdgesForTile(tile,-1)
idToPossibleEdges[id] = append(idToPossibleEdges[id],getAllPossibleEdgesForTile(tile,0)...)
idToPossibleEdges[id] = append(idToPossibleEdges[id],getAllPossibleEdgesForTile(tile,1)...)
}
for i, edge := range tile.edges {
tile.neighbourID[i] = getFittingTileForEdge(choosenTile,edge,idToPossibleEdges)
}
return tile
}
func getFittingTileForEdge(choosenTile int, edge string, idToPossibleEdges map[int][]string) int{
for id,edges := range idToPossibleEdges {
if choosenTile != id && hasMatchingEdge(edges,edge){
return id
}
}
return count
return 0
}
func hasMatchingEdge(edges []string, edge string) bool {
@ -109,3 +235,5 @@ func reverse(s string) string {
}
return string(runes)
}