99 lines
1.6 KiB
Go
99 lines
1.6 KiB
Go
package day12
|
|
|
|
import (
|
|
"log"
|
|
)
|
|
|
|
type Waypoint struct {
|
|
North, East int
|
|
}
|
|
|
|
type State2 struct {
|
|
Waypoint Waypoint
|
|
North, East int
|
|
}
|
|
|
|
func Solve2() {
|
|
state := State2{
|
|
Waypoint: Waypoint{North: 1, East: 10},
|
|
North: 0,
|
|
East: 0,
|
|
}
|
|
|
|
for _, row := range rows {
|
|
switch row.Char {
|
|
case "N":
|
|
state.Waypoint.North += row.Arg
|
|
|
|
case "S":
|
|
state.Waypoint.North -= row.Arg
|
|
|
|
case "E":
|
|
state.Waypoint.East += row.Arg
|
|
|
|
case "W":
|
|
state.Waypoint.East -= row.Arg
|
|
|
|
case "R":
|
|
switch row.Arg % 360 {
|
|
case 180:
|
|
state.Waypoint.East *= -1
|
|
state.Waypoint.North *= -1
|
|
|
|
case 90:
|
|
// Turn right a bit
|
|
n := state.Waypoint.East * -1
|
|
e := state.Waypoint.North
|
|
|
|
state.Waypoint.North = n
|
|
state.Waypoint.East = e
|
|
|
|
case 270:
|
|
// Turn left a bit
|
|
n := state.Waypoint.East
|
|
e := state.Waypoint.North * -1
|
|
|
|
state.Waypoint.North = n
|
|
state.Waypoint.East = e
|
|
}
|
|
|
|
case "L":
|
|
switch row.Arg % 360 {
|
|
case 180:
|
|
state.Waypoint.East *= -1
|
|
state.Waypoint.North *= -1
|
|
|
|
case 90:
|
|
// Turn right a bit
|
|
n := state.Waypoint.East
|
|
e := state.Waypoint.North * -1
|
|
|
|
state.Waypoint.North = n
|
|
state.Waypoint.East = e
|
|
|
|
case 270:
|
|
// Turn left a bit
|
|
n := state.Waypoint.East * -1
|
|
e := state.Waypoint.North
|
|
|
|
state.Waypoint.North = n
|
|
state.Waypoint.East = e
|
|
}
|
|
|
|
case "F":
|
|
state.North += state.Waypoint.North * row.Arg
|
|
state.East += state.Waypoint.East * row.Arg
|
|
}
|
|
}
|
|
|
|
if state.East < 0 {
|
|
state.East *= -1
|
|
}
|
|
|
|
if state.North < 0 {
|
|
state.North *= -1
|
|
}
|
|
|
|
log.Printf("2020-12-12.02: Answer: %d\n", state.North+state.East)
|
|
}
|