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.21 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.20 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.19 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.18 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.17 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.16 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.15 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.14 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.13 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.12 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.11 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.10 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.9 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.8 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.7 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.6 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.5 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.4 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.3 tracks/fsharp/exercises/word-search/Example.fs
trackler-2.0.8.2 tracks/fsharp/exercises/word-search/Example.fs