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.56 tracks/go/exercises/word-search/example.go
trackler-2.2.1.55 tracks/go/exercises/word-search/example.go
trackler-2.2.1.54 tracks/go/exercises/word-search/example.go
trackler-2.2.1.53 tracks/go/exercises/word-search/example.go
trackler-2.2.1.52 tracks/go/exercises/word-search/example.go
trackler-2.2.1.51 tracks/go/exercises/word-search/example.go
trackler-2.2.1.50 tracks/go/exercises/word-search/example.go
trackler-2.2.1.49 tracks/go/exercises/word-search/example.go
trackler-2.2.1.48 tracks/go/exercises/word-search/example.go
trackler-2.2.1.47 tracks/go/exercises/word-search/example.go
trackler-2.2.1.46 tracks/go/exercises/word-search/example.go
trackler-2.2.1.45 tracks/go/exercises/word-search/example.go
trackler-2.2.1.44 tracks/go/exercises/word-search/example.go
trackler-2.2.1.43 tracks/go/exercises/word-search/example.go
trackler-2.2.1.42 tracks/go/exercises/word-search/example.go
trackler-2.2.1.41 tracks/go/exercises/word-search/example.go
trackler-2.2.1.40 tracks/go/exercises/word-search/example.go
trackler-2.2.1.39 tracks/go/exercises/word-search/example.go
trackler-2.2.1.38 tracks/go/exercises/word-search/example.go
trackler-2.2.1.37 tracks/go/exercises/word-search/example.go