Sha256: 15f8473adcc77da3457a34337f6041d3dea15cd86e24e68613fe7b0fddf41125

Contents?: true

Size: 1.04 KB

Versions: 322

Compression:

Stored size: 1.04 KB

Contents

module WordSearch

let directions =
    [( 1,  0);
     ( 0,  1);
     (-1,  0);
     ( 0, -1);
     ( 1,  1);
     ( 1, -1);
     (-1,  1);
     (-1, -1)]
     
let update (x, y) (dx, dy) = x + dx, y + dy

let find (puzzle: string list) (word: string) =
    let width = puzzle.[0].Length
    let height = puzzle.Length

    let findChar (x, y) = 
        if x > 0 && x <= width && y > 0 && y <= height then Some puzzle.[y - 1].[x - 1]
        else None
        
    let findWord start direction =
        let rec helper coord last (remainder: string) =
            if remainder.Length = 0 then Some (start, last)
            elif Some remainder.[0] = findChar coord then helper (update coord direction) coord remainder.[1..]
            else None

        helper start start word

    seq { for x in 1 .. width do
            for y in 1 .. height do
                for dir in directions do
                    let result = findWord (x, y) dir
                    if Option.isSome result then
                        yield Option.get result }
    |> Seq.tryHead

Version data entries

322 entries across 322 versions & 1 rubygems

Version Path
trackler-2.0.8.1 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.7.0 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.44 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.43 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.42 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.41 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.40 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.39 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.38 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.37 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.36 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.35 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.34 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.33 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.32 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.31 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.30 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.29 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.28 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.27 tracks/fsharp/exercises/word-search/Example.fs