Skip to content

Commit

Permalink
Solution day 12 part 1,2.. they are ugly, will refactor later... need…
Browse files Browse the repository at this point in the history
… sleep NOW
  • Loading branch information
janetschel committed Dec 12, 2020
1 parent 6a88c22 commit 370b9e1
Show file tree
Hide file tree
Showing 3 changed files with 956 additions and 6 deletions.
118 changes: 115 additions & 3 deletions calendar/day-12/day12.go
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
}
74 changes: 71 additions & 3 deletions calendar/day-12/day12_pt02.go
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
}
Loading

0 comments on commit 370b9e1

Please sign in to comment.