Browse Source

Day12

master
Karl Spickermann 1 year ago
parent
commit
d9b66adc94
  1. 41
      .idea/workspace.xml
  2. 141
      day12/day12.go
  3. 771
      day12/day12Input.txt
  4. 5
      day12/day12Test.txt

41
.idea/workspace.xml

@ -11,11 +11,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/day11/day11.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day11/day11Test.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day11/day11Test2.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day11/day11Test3.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day11/everythingOccupied.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day12/day12.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/day12/day12Test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/helper/helper.go" beforeDir="false" afterPath="$PROJECT_DIR$/helper/helper.go" afterDir="false" />
</list>
@ -58,7 +55,7 @@
<recent name="C:\Users\karls\go\src\AoC2020" />
</key>
</component>
<component name="RunManager" selected="Go Build.go build day11.go">
<component name="RunManager" selected="Go Build.go build day12.go">
<configuration name="go build day10.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day10" />
@ -79,6 +76,16 @@
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build day12.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="AoC2020" />
<working_directory value="$PROJECT_DIR$/day12" />
<parameters value="day12Input.txt" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/day12/day12.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" />
@ -194,9 +201,11 @@
<item itemvalue="Go Build.go build day9.go" />
<item itemvalue="Go Build.go build day10.go" />
<item itemvalue="Go Build.go build day11.go" />
<item itemvalue="Go Build.go build day12.go" />
</list>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build day12.go" />
<item itemvalue="Go Build.go build day1.go" />
</list>
</recent_temporary>
@ -218,11 +227,11 @@
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="351" y="0" width="1092" height="1003" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1607708739295">
<state x="351" y="0" width="1092" height="1027" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1607788436747">
<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="1003" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607708739295" />
<state x="351" y="0" width="1092" height="1027" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607788436747" />
<state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -231,26 +240,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="386" key="GridCell.Tab.0.bottom" timestamp="1607708721938">
<state width="1877" height="386" key="GridCell.Tab.0.bottom" timestamp="1607788420906">
<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="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607708721938" />
<state width="1877" height="386" key="GridCell.Tab.0.center" timestamp="1607708721938">
<state width="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607788420906" />
<state width="1877" height="386" key="GridCell.Tab.0.center" timestamp="1607788420906">
<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="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607708721938" />
<state width="1877" height="386" key="GridCell.Tab.0.left" timestamp="1607708721938">
<state width="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607788420906" />
<state width="1877" height="386" key="GridCell.Tab.0.left" timestamp="1607788420906">
<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="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607708721938" />
<state width="1877" height="386" key="GridCell.Tab.0.right" timestamp="1607708721938">
<state width="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607788420906" />
<state width="1877" height="386" key="GridCell.Tab.0.right" timestamp="1607788420906">
<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="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607708721938" />
<state width="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607788420906" />
<state x="782" y="240" key="RollbackChangesDialog" timestamp="1607174263188">
<screen x="0" y="0" width="1920" height="1040" />
</state>

141
day12/day12.go

@ -0,0 +1,141 @@
package main
import (
"AoC2020/helper"
"fmt"
"os"
"strconv"
)
var Pointing rune
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v",input)
runPart2(input)
}
func run(input []string) {
Pointing = 'E'
position := [2]int{0,0}
for _, val := range input {
fmt.Println(val)
position = move(position,val)
fmt.Printf("Positon %v, Direction %v \n", position, string(Pointing))
}
}
func runPart2(input []string) {
positionBoat := [2]int{0,0}
positionWayPoint := [2]int{10,1}
for _, val := range input {
fmt.Println(val)
positionBoat, positionWayPoint = movePart2(positionBoat,positionWayPoint,val)
fmt.Printf("PositonBoat: %v, PositionWaypoint %v \n", positionBoat, positionWayPoint)
}
}
func move(position [2]int, command string) [2]int{
direction := rune(command[0])
distance, _ := strconv.Atoi(command[1:])
if direction == 'L' || direction == 'R' {
Pointing = turnByDegree(Pointing,direction,distance)
return position
}
if direction == 'F'{
newCommand := string(Pointing) + command[1:]
return move(position,newCommand)
}
return moveCompass(position,direction,distance)
}
func movePart2(positionBoat [2]int, positionWayPoint [2]int, command string) ([2]int, [2]int){
direction := rune(command[0])
if direction == 'F'{
distance, _ := strconv.Atoi(command[1:])
var newPositionBoat [2]int
newPositionBoat[0] = positionBoat[0] + distance * positionWayPoint[0]
newPositionBoat[1] = positionBoat[1] + distance * positionWayPoint[1]
positionBoat = newPositionBoat
}else {
newPositionWaypoint := moveWayPoint(positionWayPoint,command)
positionWayPoint = newPositionWaypoint
}
return positionBoat, positionWayPoint
}
func moveCompass(position [2]int, direction rune, distance int) [2]int {
switch direction {
case 'N':return [2]int{position[0], position[1] + distance}
case 'S':return [2]int{position[0], position[1] - distance}
case 'E':return [2]int{position[0] + distance, position[1]}
case 'W':return [2]int{position[0] - distance, position[1]}
}
return position
}
func moveWayPoint(position [2]int, command string) [2]int {
direction := rune(command[0])
distance, _ := strconv.Atoi(command[1:])
if direction == 'L' || direction == 'R' {
return turnWayPoint(position,direction,distance)
}
return moveCompass(position,direction,distance)
}
func turnWayPoint(position [2]int,turn rune, degree int) [2]int {
p0 := position[0]
p1 := position[1]
var newDs = make([]rune,2)
if p0 > 0 {
newDs[0] = turnByDegree('E', turn, degree)
}else {
newDs[0] = turnByDegree('W', turn, degree)
}
if p1 > 0 {
newDs[1] = turnByDegree('N', turn, degree)
}else {
newDs[1] = turnByDegree('S', turn, degree)
}
var newPosition [2]int
for i,val := range newDs {
switch val {
case 'N': newPosition[1] = Abs(position[i])
case 'S': newPosition[1] = -Abs(position[i])
case 'E': newPosition[0] = Abs(position[i])
case 'W': newPosition[0] = -Abs(position[i])
}
}
return newPosition
}
func turnByDegree(pointing rune, turn rune, degree int) rune {
directions := []rune{'N','E','S','W'}
move := degree / 90
if turn == 'L' {
move = -1 * move
}
for i, val := range directions {
if val == pointing {
newDirectionPointer := (i + move) % len(directions)
if newDirectionPointer < 0 {
newDirectionPointer = len(directions) + newDirectionPointer
}
return directions[newDirectionPointer]
}
}
return pointing
}
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}

771
day12/day12Input.txt

@ -0,0 +1,771 @@
F92
R180
S1
F64
R90
S1
E1
F11
N4
R180
S3
E3
F55
R90
N1
E4
L180
F9
N3
R90
W4
N4
F36
L90
F50
E3
F9
E5
S1
W2
E4
R180
N3
F18
N2
S5
W1
F41
E3
N4
F66
R270
F22
N5
E4
N1
L90
W1
N1
R90
F87
E1
N4
E1
L90
E5
L180
F84
S5
F65
N2
W1
F65
N3
L90
R90
E1
L180
S1
F60
E3
L90
F22
E5
F32
S1
L90
E2
S2
W5
N2
E5
F60
L180
W3
F49
S5
F84
N5
F78
W5
S3
L90
S3
F83
L90
E1
L90
E1
F99
W1
L90
W2
L90
S1
R180
F8
E2
R180
N2
F66
N5
L90
E1
F76
W3
F23
F79
N3
E1
R90
N5
F33
N2
L180
F97
R90
L90
S5
F99
W5
L90
F47
L90
E4
F99
S4
R90
E2
S2
L90
F33
E2
F71
R90
N1
N2
W3
L90
W4
S1
R90
F27
L270
W5
F49
W3
F41
L90
F23
L90
S1
F11
L90
W3
N1
W4
L90
N4
W2
L90
S3
W4
L180
W4
R180
W4
F49
E3
S2
F64
S4
F61
E3
L180
W1
N2
W2
F64
R90
S4
E5
N1
L270
E3
F22
E2
R90
S5
F57
N2
F78
L270
W1
N4
L90
S4
F71
S2
L90
E1
L180
F4
N1
W4
L90
N5
F98
N3
L90
W2
L180
F54
W1
R90
W3
F67
L90
W2
R180
F79
N4
F30
L90
N4
R180
L90
S5
F7
N2
F26
S5
F43
E3
N3
L180
F57
W4
L90
E5
F100
S2
F86
L180
W3
S5
W1
S3
L180
S2
S5
W4
L90
F73
R90
F14
E2
F99
R90
R90
E5
N1
R90
N1
N4
R180
S4
L90
F98
W2
L90
F100
R180
W4
F92
E1
L180
S3
F94
E4
N5
R90
S3
W4
F86
S5
R90
E2
F6
E2
F75
N1
L90
W4
F2
E1
N4
E1
L90
W4
F17
N2
R90
E5
R90
N1
W1
R180
W4
R180
F97
E5
N2
L90
F27
N1
W5
R90
N1
F98
R90
N2
F12
W5
S1
F86
N3
W3
F71
L180
W1
R90
W3
S4
E2
S5
W3
R90
E4
R90
W1
N3
F11
W4
E4
F67
E2
F15
W5
N4
W1
R90
L90
W4
F16
N4
F87
L180
F73
L180
E5
R90
F96
N5
W1
F28
R90
W5
S3
S3
E5
R180
E4
F4
L90
E3
E4
S5
W3
L180
F57
R90
N2
F78
N2
L90
S1
R90
W4
S1
L90
N5
F60
R90
S4
F42
R90
W2
F31
R180
N2
S2
F91
S4
R90
N2
L180
N1
E4
F72
E3
E3
N2
L90
N1
L90
F83
S1
R90
N5
E4
F53
N3
E5
S3
E4
F93
W2
F53
L90
E2
L90
W2
F96
R90
E3
L90
S5
F31
R90
R90
F98
F91
W2
N3
F69
R90
F51
R90
N1
E2
R90
S1
R180
E5
N5
W5
F34
W1
F60
R90
F53
R90
F62
R90
S4
F95
W5
E2
F84
R90
E2
F54
E2
F5
R90
F32
L90
E2
S2
L180
F31
E2
L90
N5
W2
L90
F53
R90
S1
R90
F49
R90
S3
L90
F76
F31
S1
F13
R90
W1
S1
R90
W2
L180
E2
R90
E2
N1
W1
S1
E1
S4
L90
N2
E2
R90
S4
L90
N5
L90
W4
S2
W1
L90
E4
R90
W2
S1
L90
N5
F63
R90
W1
F20
L90
F86
S4
S4
E2
F92
F90
W4
F94
E1
N5
F30
W3
N3
L180
W5
F52
R270
E5
R90
N4
W4
F10
N1
F16
N1
F31
S4
L90
F81
N1
L90
W2
F84
L90
F79
L90
W5
L90
F90
R90
E2
L90
F25
N5
S1
F12
R90
F34
R90
F4
E4
L90
S1
F70
R90
F22
R90
F67
R90
F40
W1
S3
L180
N2
E4
S4
F98
R180
F16
S2
W4
S1
F70
L90
N3
E3
L270
W4
L90
W5
R180
W5
N4
R90
W4
F5
L270
E3
L90
E5
F50
S3
F51
E1
S5
F95
R90
L90
S2
F75
R90
W2
F70
E2
F52
W4
E1
L180
F72
N2
F5
E2
F94
R180
F7
E3
N2
R90
W1
N4
R90
S1
W4
L270
W1
W1
N2
E1
F31
N1
W4
S5
L90
E1
R180
F50
N1
W2
F66
N2
L90
R90
W1
E5
L90
E2
R180
F63
N4
N3
F87
W4
L90
F85
N3
F93
S2
F95
N5
L180
S3
R90
W5
L180
W1
L90
S4
F68
L180
S4
R180
E2
L180
E3
R180
S5
F88
N1
R90
S3
E5
S5
E1
N5
E2
S1
W5
F62
R90
F39
L180
N3
F42
E4
N5
R90
W5
S2
F43
W4
F94
R90
F85
N5
F7
L180
W4
L180
E4
F44
N2
W2
N3
L180
E3
N1
S2
W4
N5
R180
F78
E5
L90
W4
F77

5
day12/day12Test.txt

@ -0,0 +1,5 @@
F10
N3
F7
R90
F11
Loading…
Cancel
Save