-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution day 12 part 1,2.. they are ugly, will refactor later... need…
… sleep NOW
- Loading branch information
1 parent
6a88c22
commit 370b9e1
Showing
3 changed files
with
956 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 1,128 @@ | ||
package main | ||
|
||
import "advent-of-go-2020/utils/files" | ||
import ( | ||
"advent-of-go-2020/utils/conv" | ||
"advent-of-go-2020/utils/files" | ||
) | ||
|
||
func main() { | ||
input := files.ReadFile(12, "\n") | ||
println(solvePart1(input)) | ||
} | ||
|
||
func solvePart1(input []string) int { | ||
result := 0 | ||
x, y, facing := 0,0, "E" | ||
|
||
for _, element := range input { | ||
instruction := element[:1] | ||
amount := conv.ToInt(element[1:]) | ||
|
||
x, y, facing = move(instruction, amount, facing, x, y) | ||
} | ||
|
||
return result | ||
if x < 0 { | ||
x *= -1 | ||
} | ||
|
||
if y < 0 { | ||
y *= - 1 | ||
} | ||
|
||
return x y | ||
} | ||
|
||
func move(instruction string, amount int, facing string, x int, y int) (int, int, string) { | ||
if instruction == "F" { | ||
if facing == "E" { | ||
x = amount | ||
} else if facing == "N" { | ||
y -= amount | ||
} else if facing == "S" { | ||
y = amount | ||
} else if facing == "W"{ | ||
x -= amount | ||
} | ||
} else if instruction == "N" { | ||
y -= amount | ||
} else if instruction == "S" { | ||
y = amount | ||
} else if instruction == "E" { | ||
x = amount | ||
} else if instruction == "W" { | ||
x -= amount | ||
} else if instruction == "R" { | ||
times := amount % 89 | ||
|
||
if facing == "E" { | ||
if times == 1 { | ||
facing = "S" | ||
} else if times == 2 { | ||
facing = "W" | ||
} else if times == 3 { | ||
facing = "N" | ||
} | ||
} else if facing == "S" { | ||
if times == 1 { | ||
facing = "W" | ||
} else if times == 2 { | ||
facing = "N" | ||
} else if times == 3 { | ||
facing = "E" | ||
} | ||
} else if facing == "N" { | ||
if times == 1 { | ||
facing = "E" | ||
} else if times == 2 { | ||
facing = "S" | ||
} else if times == 3 { | ||
facing = "W" | ||
} | ||
} else if facing == "W" { | ||
if times == 1 { | ||
facing = "N" | ||
} else if times == 2 { | ||
facing = "E" | ||
} else if times == 3 { | ||
facing = "S" | ||
} | ||
} | ||
} else if instruction == "L" { | ||
times := amount % 89 | ||
if facing == "E" { | ||
if times == 1 { | ||
facing = "N" | ||
} else if times == 2 { | ||
facing = "W" | ||
} else if times == 3 { | ||
facing = "S" | ||
} | ||
} else if facing == "S" { | ||
if times == 1 { | ||
facing = "E" | ||
} else if times == 2 { | ||
facing = "N" | ||
} else if times == 3 { | ||
facing = "W" | ||
} | ||
} else if facing == "N" { | ||
if times == 1 { | ||
facing = "W" | ||
} else if times == 2 { | ||
facing = "S" | ||
} else if times == 3 { | ||
facing = "E" | ||
} | ||
} else if facing == "W" { | ||
if times == 1 { | ||
facing = "S" | ||
} else if times == 2 { | ||
facing = "E" | ||
} else if times == 3 { | ||
facing = "N" | ||
} | ||
} | ||
|
||
} | ||
|
||
return x,y,facing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 1,86 @@ | ||
package main | ||
|
||
import ( | ||
"advent-of-go-2020/utils/conv" | ||
"advent-of-go-2020/utils/files" | ||
) | ||
|
||
func main() { | ||
input := files.ReadFile(12, "\n") | ||
println(solvePart1(input)) | ||
println(solvePart2(input)) | ||
} | ||
|
||
func solvePart2(input []string) int { | ||
result := 0 | ||
x, y, facing := 0,0, "E" | ||
xW, yW := 10, -1 | ||
|
||
for _, element := range input { | ||
instruction := element[:1] | ||
amount := conv.ToInt(element[1:]) | ||
|
||
x, y, xW, yW, facing = moveWaypoint(instruction, amount, facing, x, y, xW, yW) | ||
println("-----------------") | ||
println(x) | ||
println(y) | ||
println(xW) | ||
println(yW) | ||
} | ||
|
||
return result | ||
if x < 0 { | ||
x *= -1 | ||
} | ||
|
||
if y < 0 { | ||
y *= - 1 | ||
} | ||
|
||
return x y | ||
} | ||
|
||
func moveWaypoint(instruction string, amount int, facing string, x int, y int, xW int, yW int) (int, int, int, int, string) { | ||
if instruction == "F" { | ||
x = xW * amount | ||
y = yW * amount | ||
} else if instruction == "N" { | ||
yW -= amount | ||
} else if instruction == "S" { | ||
yW = amount | ||
} else if instruction == "E" { | ||
xW = amount | ||
} else if instruction == "W" { | ||
xW -= amount | ||
} else if instruction == "R" { | ||
times := (amount / 90) % 4 | ||
|
||
if times == 1 { | ||
oldX := xW | ||
xW = -yW | ||
yW = oldX | ||
} else if times == 2 { | ||
xW = -xW | ||
yW = -yW | ||
} else if times == 3 { | ||
oldX := xW | ||
xW = yW | ||
yW = -oldX | ||
} | ||
|
||
} else if instruction == "L" { | ||
times := ((-amount / 90) 4) % 4 | ||
if times == 1 { | ||
oldX := xW | ||
xW = -yW | ||
yW = oldX | ||
} else if times == 2 { | ||
xW = -xW | ||
yW = -yW | ||
} else if times == 3 { | ||
oldX := xW | ||
xW = yW | ||
yW = -oldX | ||
} | ||
|
||
} | ||
|
||
return x,y, xW, yW, facing | ||
} |
Oops, something went wrong.