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.6.26 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.25 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.24 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.23 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.22 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.21 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.20 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.19 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.18 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.17 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.16 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.15 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.14 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.13 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.12 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.11 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.10 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.9 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.8 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.6.7 tracks/fsharp/exercises/word-search/Example.fs