Browse Source

Day17

master
Karl Spickermann 1 year ago
parent
commit
6194da02c9
  1. 43
      .idea/workspace.xml
  2. 167
      day17/day17.go
  3. 8
      day17/day17Input.txt
  4. 3
      day17/day17Test.txt

43
.idea/workspace.xml

@ -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$/day16/day16.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day16/day16Test.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day16/day16Test2.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day17/day17.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day17/day17Test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -55,7 +54,7 @@
<recent name="C:\Users\karls\go\src\AoC2020" />
</key>
</component>
<component name="RunManager" selected="Go Build.go build day16.go">
<component name="RunManager" selected="Go Build.go build day17.go">
<configuration name="go build day10.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day10" />
@ -125,6 +124,16 @@
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build day17.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day17" />
<parameters value="day17Input.txt" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/day17/day17.go" />
<package value="AoC2020" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build day1.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day1" />
@ -245,9 +254,11 @@
<item itemvalue="Go Build.go build day14.go" />
<item itemvalue="Go Build.go build day15.go" />
<item itemvalue="Go Build.go build day16.go" />
<item itemvalue="Go Build.go build day17.go" />
</list>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build day17.go" />
<item itemvalue="Go Build.go build day1.go" />
</list>
</recent_temporary>
@ -269,11 +280,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="1608119340704">
<state x="351" y="0" width="1092" height="1040" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1608236055902">
<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="1608119340704" />
<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="1608236055902" />
<state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -282,26 +293,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="1877" height="450" key="GridCell.Tab.0.bottom" timestamp="1608119343074">
<state width="1877" height="426" key="GridCell.Tab.0.bottom" timestamp="1608236028399">
<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="1877" height="450" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608119343074" />
<state width="1877" height="450" key="GridCell.Tab.0.center" timestamp="1608119343074">
<state width="1877" height="426" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608236028399" />
<state width="1877" height="426" key="GridCell.Tab.0.center" timestamp="1608236028399">
<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="1877" height="450" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608119343074" />
<state width="1877" height="450" key="GridCell.Tab.0.left" timestamp="1608119343074">
<state width="1877" height="426" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608236028399" />
<state width="1877" height="426" key="GridCell.Tab.0.left" timestamp="1608236028399">
<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="1877" height="450" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608119343074" />
<state width="1877" height="450" key="GridCell.Tab.0.right" timestamp="1608119343074">
<state width="1877" height="426" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608236028399" />
<state width="1877" height="426" key="GridCell.Tab.0.right" timestamp="1608236028399">
<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="1877" height="450" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608119343074" />
<state width="1877" height="426" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608236028399" />
<state x="782" y="240" key="RollbackChangesDialog" timestamp="1607174263188">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -318,10 +329,10 @@
<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="1608118956144">
<state x="593" y="141" width="585" height="801" key="find.popup" timestamp="1608235621382">
<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="1608118956144" />
<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="1608235621382" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1608045273455">
<screen x="0" y="0" width="1920" height="1040" />
</state>

167
day17/day17.go

@ -0,0 +1,167 @@
package main
import (
"AoC2020/helper"
"fmt"
"os"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
dimension := readInputLayer(input,0)
fmt.Println(len(getDirectionsPart2()))
for i := 0; i < 6; i++ {
dimension = run(dimension, getDirectionsPart2)
}
}
func run(dimension map[[4]int]rune, getDirections func() [][4]int) map[[4]int]rune {
var changedValues = make(map[[4]int]rune)
directions := getDirections()
visitedNeighbours := make(map[[4]int]int)
for position, status := range dimension {
neighbourPosition := getNeighbourPoints(position,directions)
neighbours := getActiveNeighbours(dimension, neighbourPosition)
if status == '#' && !(neighbours == 2 || neighbours == 3) {
changedValues[position] = '.'
}
for _, position := range neighbourPosition {
visitedNeighbours[position] ++
}
}
for position, val := range visitedNeighbours {
if val == 3 {
changedValues[position] = '#'
}
}
for position, status := range changedValues {
//fmt.Printf("Changed Position: %v Status: %v \n",position,string(status))
if status == '.' {
delete(dimension, position)
}
if status == '#' {
dimension[position] = status
}
}
fmt.Printf("Active Points: %v \n", len(dimension))
return dimension
}
func readInputLayer(input []string, layerLevel int) map[[4]int]rune {
var layer = make(map[[4]int]rune)
for y, row := range input {
for x, val := range row {
if val == '#' {
layer[[4]int{y,x,layerLevel,0}] = val
}
}
}
return layer
}
func printLayer(layer int, width int, height int, dimension map[[3]int]rune) {
for i:= 0; i < height; i++ {
var row []rune
for j:= 0; j < width; j++ {
val := dimension[[3]int{i,j,layer}]
if val == '#' {
row = append(row,val)
}else {
row = append(row, '.')
}
}
fmt.Println(string(row))
}
fmt.Println()
}
func getNeighbourPoints(position [4]int, directions [][4]int) [][4]int{
var neighbourPositions [][4]int
for _, direction := range directions {
neighbourPositions = append(neighbourPositions, add(position, direction))
}
return neighbourPositions
}
func getActiveNeighbours(dimension map[[4]int]rune, neighbourPositions [][4]int) int {
activeNeighbours := 0
for _, neighbourPosition := range neighbourPositions {
if dimension[neighbourPosition] == '#' {
activeNeighbours++
}
}
//fmt.Printf("Active Neighbours: %v \n" ,activeNeighbours)
return activeNeighbours
}
func add(a [4]int, b [4]int) [4]int {
return [4]int{a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]}
}
func getDirections() [][4]int {
possibleValues := [3]int{-1, 0, 1}
var directions [][4]int
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
for k := 0; k < 3; k++ {
if possibleValues[i] != 0 || possibleValues[j] != 0 || possibleValues[k] != 0 {
directions = append(directions, [4]int{possibleValues[i], possibleValues[j], possibleValues[k],0})
}
}
}
}
return directions
}
func getDirectionsPart2() [][4]int {
possibleValues := [3]int{-1, 0, 1}
var directions [][4]int
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
for k := 0; k < 3; k++ {
for l := 0; l < 3; l++ {
if possibleValues[i] != 0 || possibleValues[j] != 0 || possibleValues[k] != 0 || possibleValues[l] != 0 {
directions = append(directions, [4]int{possibleValues[i], possibleValues[j], possibleValues[k],possibleValues[l]})
}
}
}
}
}
return directions
}
func getStatus(position [3]int, dimension [][][]rune) rune {
if len(dimension) < position[0] {
return '.'
}
layer := dimension[position[0]]
if len(layer) < position[1] {
return '.'
}
row := layer[position[1]]
if len(row) < position[2] {
return '.'
}
return rune(row[position[2]])
}
func initiateDimension(z int, x int, y int) [][][]rune {
var emptyRow []rune
for i := 0; i < x; i++ {
emptyRow = append(emptyRow, '.')
}
var emptyLayer [][]rune
for i := 0; i < y; i++ {
emptyLayer = append(emptyLayer, emptyRow)
}
var emptyDimension [][][]rune
for i := 0; i < z; i++ {
emptyDimension = append(emptyDimension, emptyLayer)
}
return emptyDimension
}

8
day17/day17Input.txt

@ -0,0 +1,8 @@
##....#.
#.#..#..
...#....
...#.#..
###....#
#.#....#
.#....##
.#.###.#

3
day17/day17Test.txt

@ -0,0 +1,3 @@
.#.
..#
###
Loading…
Cancel
Save