1package wordsearch23// Direction represents one of 8 possible directions a word can be placed4type Direction int56const (7 Right Direction = iota8 Down9 DownRight10 DownLeft11 Left12 Up13 UpRight14 UpLeft15)1617// Vector returns the dx, dy unit vector for a direction18func (d Direction) Vector() (int, int) {19 switch d {20 case Right:21 return 1, 022 case Down:23 return 0, 124 case DownRight:25 return 1, 126 case DownLeft:27 return -1, 128 case Left:29 return -1, 030 case Up:31 return 0, -132 case UpRight:33 return 1, -134 case UpLeft:35 return -1, -136 default:37 return 0, 038 }39}4041// Position represents a coordinate in the grid42type Position struct {43 X, Y int44}4546// Word represents a hidden word in the puzzle47type Word struct {48 Text string49 Start Position50 End Position51 Direction Direction52 Found bool53}5455// Contains checks if the given position is part of this word56func (w *Word) Contains(pos Position) bool {57 dx, dy := w.Direction.Vector()58 x, y := w.Start.X, w.Start.Y5960 for i := 0; i < len(w.Text); i++ {61 if x == pos.X && y == pos.Y {62 return true63 }64 x += dx65 y += dy66 }67 return false68}6970// Positions returns all positions occupied by this word71func (w *Word) Positions() []Position {72 positions := make([]Position, 0, len(w.Text))73 dx, dy := w.Direction.Vector()74 x, y := w.Start.X, w.Start.Y7576 for i := 0; i < len(w.Text); i++ {77 positions = append(positions, Position{X: x, Y: y})78 x += dx79 y += dy80 }81 return positions82}