Day20 Part1

This commit is contained in:
Karl Spickermann 2020-12-20 17:09:40 +01:00
parent b315cdff0e
commit 7961849040
4 changed files with 1974 additions and 19 deletions

View File

@ -11,8 +11,8 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment=""> <list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/day19/day19.go" afterDir="false" /> <change afterPath="$PROJECT_DIR$/day20/day20.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day19/day19Test.txt" 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$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -54,7 +54,7 @@
<recent name="C:\Users\karls\go\src\AoC2020" /> <recent name="C:\Users\karls\go\src\AoC2020" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Go Build.go build day19.go"> <component name="RunManager" selected="Go Build.go build day20.go">
<configuration name="go build day10.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true"> <configuration name="go build day10.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" /> <module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day10" /> <working_directory value="$PROJECT_DIR$/day10" />
@ -124,7 +124,7 @@
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="go build day17.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true"> <configuration name="go build day17.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" /> <module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day17" /> <working_directory value="$PROJECT_DIR$/day17" />
<parameters value="day17Input.txt" /> <parameters value="day17Input.txt" />
@ -175,6 +175,16 @@
<output_directory value="$PROJECT_DIR$/day1" /> <output_directory value="$PROJECT_DIR$/day1" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<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" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/day20/day20.go" />
<package value="AoC2020" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build day2.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true"> <configuration name="go build day2.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" /> <module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day2" /> <working_directory value="$PROJECT_DIR$/day2" />
@ -277,10 +287,10 @@
<item itemvalue="Go Build.go build day18.go" /> <item itemvalue="Go Build.go build day18.go" />
<item itemvalue="Go Build.go build day19.go" /> <item itemvalue="Go Build.go build day19.go" />
<item itemvalue="Go Build.go build day17.go" /> <item itemvalue="Go Build.go build day17.go" />
<item itemvalue="Go Build.go build day20.go" />
</list> </list>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Go Build.go build day17.go" />
<item itemvalue="Go Build.go build day1.go" /> <item itemvalue="Go Build.go build day1.go" />
</list> </list>
</recent_temporary> </recent_temporary>
@ -302,11 +312,11 @@
<option name="oldMeFiltersMigrated" value="true" /> <option name="oldMeFiltersMigrated" value="true" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1608420472026"> <state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1608477052965">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="-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="1608420472026" /> <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="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379"> <state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
@ -315,30 +325,30 @@
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state x="739" y="287" key="FileChooserDialogImpl/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607278759015" /> <state x="739" y="287" key="FileChooserDialogImpl/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607278759015" />
<state width="1173" height="303" key="GridCell.Tab.0.bottom" timestamp="1608421505598"> <state width="1172" height="347" key="GridCell.Tab.0.bottom" timestamp="1608480561089">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
<state width="1173" height="303" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608421505598" /> <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="1173" height="303" key="GridCell.Tab.0.center" timestamp="1608421505598"> <state width="1172" height="347" key="GridCell.Tab.0.center" timestamp="1608480561089">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
<state width="1173" height="303" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608421505598" /> <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="1173" height="303" key="GridCell.Tab.0.left" timestamp="1608421505598"> <state width="1172" height="347" key="GridCell.Tab.0.left" timestamp="1608480561089">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
<state width="1173" height="303" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608421505598" /> <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="1173" height="303" key="GridCell.Tab.0.right" timestamp="1608421505598"> <state width="1172" height="347" key="GridCell.Tab.0.right" timestamp="1608480561089">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
<state width="1173" height="303" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608421505598" /> <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 x="782" y="240" key="RollbackChangesDialog" timestamp="1607174263188"> <state x="782" y="240" key="RollbackChangesDialog" timestamp="1608474837005">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state x="782" y="240" key="RollbackChangesDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607174263188" /> <state x="782" y="240" key="RollbackChangesDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608474837005" />
<state x="461" y="165" key="SettingsEditor" timestamp="1607081830252"> <state x="461" y="165" key="SettingsEditor" timestamp="1607081830252">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
@ -351,10 +361,10 @@
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="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="1608417937072"> <state x="593" y="141" width="585" height="801" key="find.popup" timestamp="1608475300269">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </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="1608417937072" /> <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="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1608045273455">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>

111
day20/day20.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"AoC2020/helper"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
var lineBreak = "\n"
type tile struct {
neighbourID [4]int
edges [4]string
}
func main() {
args := os.Args[1:]
input, err := helper.GetFile(args[0])
if err != nil {
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
}
fmt.Println(product)
}
func getTiles(input []string) map[int][4]string{
var tiles = make(map[int][4]string)
for _, val := range input {
id, edges := getTile(val)
fmt.Printf("ID:%v \n",id )
tiles[id] = edges
}
return tiles
}
func getTile(input string) (int, [4]string) {
tilesRow := strings.Split(input, lineBreak)
var edges [4]string
edges[0] = tilesRow[1]
edges[2] = tilesRow[len(tilesRow)-1]
for _, val := range tilesRow[1:] {
edges[3] = edges[3]+val[0:1]
edges[1] = edges[1]+val[len(val)-1:]
}
r, _ := regexp.Compile("[0-9]{4}")
number, _ := strconv.Atoi(r.FindString(tilesRow[0]))
return number, edges
}
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))
}
}
}
return edges
}
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)
}
}
return cornerIds
}
func countMatchingEges(allEdges []string, tileEdges [4]string) int{
count := 0
for _, val := range tileEdges{
if hasMatchingEdge(allEdges,val){
count ++
}
}
return count
}
func hasMatchingEdge(edges []string, edge string) bool {
for _, val := range edges {
if val == edge{
return true
}
}
return false
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}

1727
day20/day20Input.txt Normal file

File diff suppressed because it is too large Load Diff

107
day20/day20Test.txt Normal file
View File

@ -0,0 +1,107 @@
Tile 2311:
..##.#..#.
##..#.....
#...##..#.
####.#...#
##.##.###.
##...#.###
.#.#.#..##
..#....#..
###...#.#.
..###..###
Tile 1951:
#.##...##.
#.####...#
.....#..##
#...######
.##.#....#
.###.#####
###.##.##.
.###....#.
..#.#..#.#
#...##.#..
Tile 1171:
####...##.
#..##.#..#
##.#..#.#.
.###.####.
..###.####
.##....##.
.#...####.
#.##.####.
####..#...
.....##...
Tile 1427:
###.##.#..
.#..#.##..
.#.##.#..#
#.#.#.##.#
....#...##
...##..##.
...#.#####
.#.####.#.
..#..###.#
..##.#..#.
Tile 1489:
##.#.#....
..##...#..
.##..##...
..#...#...
#####...#.
#..#.#.#.#
...#.#.#..
##.#...##.
..##.##.##
###.##.#..
Tile 2473:
#....####.
#..#.##...
#.##..#...
######.#.#
.#...#.#.#
.#########
.###.#..#.
########.#
##...##.#.
..###.#.#.
Tile 2971:
..#.#....#
#...###...
#.#.###...
##.##..#..
.#####..##
.#..####.#
#..#.#..#.
..####.###
..#.#.###.
...#.#.#.#
Tile 2729:
...#.#.#.#
####.#....
..#.#.....
....#..#.#
.##..##.#.
.#.####...
####.#.#..
##.####...
##..#.##..
#.##...##.
Tile 3079:
#.#.#####.
.#..######
..#.......
######....
####.#..#.
.#...#.##.
#.#####.##
..#.###...
..#.......
..#.###...