mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-21 18:00:47 +00:00
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type coords struct {
|
||
|
X int
|
||
|
Y int
|
||
|
}
|
||
|
|
||
|
func newCoords(x, y int) coords {
|
||
|
return coords{X: x, Y: y}
|
||
|
}
|
||
|
|
||
|
// node represents a single node with coordinates and direction
|
||
|
type node struct {
|
||
|
coords
|
||
|
direction int
|
||
|
}
|
||
|
|
||
|
func newNode(x, y int, direction int) node {
|
||
|
return node{coords: newCoords(x, y), direction: direction}
|
||
|
}
|
||
|
|
||
|
// String implements the fmt.Stringer interface
|
||
|
func (n node) String() string {
|
||
|
return fmt.Sprintf("%v%s", n.coords, dirs[n.direction])
|
||
|
}
|
||
|
|
||
|
type mover struct {
|
||
|
node
|
||
|
nodes []node
|
||
|
}
|
||
|
|
||
|
// newMover sets the start coordinates and direction
|
||
|
// it returns a mover type
|
||
|
func newMover(n node) *mover {
|
||
|
return &mover{node: n, nodes: make([]node, 0)}
|
||
|
}
|
||
|
|
||
|
// direction returns the current node direction
|
||
|
func (m *mover) direction() int {
|
||
|
return m.node.direction
|
||
|
}
|
||
|
|
||
|
// setDirection sets the current node direction
|
||
|
func (m *mover) setDirection(val int) {
|
||
|
m.node.direction = val
|
||
|
}
|
||
|
|
||
|
// move shifts the X,Y coordinate by one depending on direction
|
||
|
func (m *mover) move() {
|
||
|
switch m.node.direction {
|
||
|
case N:
|
||
|
m.Y--
|
||
|
case S:
|
||
|
m.Y++
|
||
|
case W:
|
||
|
m.X--
|
||
|
case E:
|
||
|
m.X++
|
||
|
}
|
||
|
}
|