Sha256: 2990016b2964bbb34b67cc5a7880f2309a7a6033d0b51164c613d7659d4a06dd

Contents?: true

Size: 1.62 KB

Versions: 274

Compression:

Stored size: 1.62 KB

Contents

package wordsearch

import "fmt"

const testVersion = 3

func Solve(words []string, puzzle []string) (map[string][2][2]int, error) {
	positions := make(map[string][2][2]int, len(words))
	for _, word := range words {
		span, err := find(word, puzzle)
		if err != nil {
			return nil, err
		}
		positions[word] = span
	}
	return positions, nil
}

type dir struct {
	cx, cy int
}

var (
	north     = dir{0, -1}
	northeast = dir{1, -1}
	east      = dir{1, 0}
	southeast = dir{1, 1}
	south     = dir{0, 1}
	southwest = dir{-1, 1}
	west      = dir{-1, 0}
	northwest = dir{-1, -1}
)

var dirs = []dir{north, northeast, east, southeast, south, southwest, west, northwest}

var zeroSpan = [2][2]int{{0, 0}, {0, 0}}

func find(word string, puzzle []string) ([2][2]int, error) {
	for x := 0; x < len(puzzle[0]); x++ {
		for y := 0; y < len(puzzle); y++ {
			for _, dir := range dirs {
				if span, ok := try(word, puzzle, x, y, dir); ok {
					return span, nil
				}
			}
		}
	}
	return zeroSpan, fmt.Errorf("Didn't find %q", word)
}

func try(word string, puzzle []string, startX, startY int, dir dir) ([2][2]int, bool) {
	x, y := startX, startY
	for _, c := range word {
		if lc, ok := lookup(x, y, puzzle); !ok || lc != c {
			return zeroSpan, false
		}
		x += dir.cx
		y += dir.cy
	}
	// Need to remove the last increment to get the position of the last character.
	return [2][2]int{{startX, startY}, {x - dir.cx, y - dir.cy}}, true
}

func lookup(x, y int, puzzle []string) (rune, bool) {
	if x >= 0 && x < len(puzzle[0]) && y >= 0 && y < len(puzzle) {
		// Yes, yes, this won't work with multi-byte codepoints.
		return rune(puzzle[y][x]), true
	}
	return 0, false
}

Version data entries

274 entries across 274 versions & 1 rubygems

Version Path
trackler-2.2.1.36 tracks/go/exercises/word-search/example.go
trackler-2.2.1.35 tracks/go/exercises/word-search/example.go
trackler-2.2.1.34 tracks/go/exercises/word-search/example.go
trackler-2.2.1.33 tracks/go/exercises/word-search/example.go
trackler-2.2.1.32 tracks/go/exercises/word-search/example.go
trackler-2.2.1.31 tracks/go/exercises/word-search/example.go
trackler-2.2.1.30 tracks/go/exercises/word-search/example.go
trackler-2.2.1.29 tracks/go/exercises/word-search/example.go
trackler-2.2.1.28 tracks/go/exercises/word-search/example.go
trackler-2.2.1.27 tracks/go/exercises/word-search/example.go
trackler-2.2.1.26 tracks/go/exercises/word-search/example.go
trackler-2.2.1.25 tracks/go/exercises/word-search/example.go
trackler-2.2.1.24 tracks/go/exercises/word-search/example.go
trackler-2.2.1.23 tracks/go/exercises/word-search/example.go
trackler-2.2.1.22 tracks/go/exercises/word-search/example.go
trackler-2.2.1.21 tracks/go/exercises/word-search/example.go
trackler-2.2.1.20 tracks/go/exercises/word-search/example.go
trackler-2.2.1.19 tracks/go/exercises/word-search/example.go
trackler-2.2.1.18 tracks/go/exercises/word-search/example.go
trackler-2.2.1.17 tracks/go/exercises/word-search/example.go