diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1eb48ea..265a2c7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,11 +11,8 @@ - - - - - + + @@ -58,7 +55,7 @@ - + @@ -79,6 +76,16 @@ + + + + + + + + + + @@ -194,9 +201,11 @@ + + @@ -218,11 +227,11 @@ - + - + @@ -231,26 +240,26 @@ - + - - + + - - + + - - + + - + diff --git a/day12/day12.go b/day12/day12.go new file mode 100644 index 0000000..03c5618 --- /dev/null +++ b/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 +} diff --git a/day12/day12Input.txt b/day12/day12Input.txt new file mode 100644 index 0000000..795e4a0 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/day12/day12Test.txt b/day12/day12Test.txt new file mode 100644 index 0000000..48c2a50 --- /dev/null +++ b/day12/day12Test.txt @@ -0,0 +1,5 @@ +F10 +N3 +F7 +R90 +F11 \ No newline at end of file