Sha256: aaee8779adec6d33d4e4a95eafe9d4993507fee81cd9f55302372d504a208c0d

Contents?: true

Size: 1.78 KB

Versions: 115

Compression:

Stored size: 1.78 KB

Contents

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

public class WordSearch
{
    private readonly string[] rows;
    private readonly int width;
    private readonly int height;

    private static readonly Point[] Directions =
    {
        new Point( 1,  0),
        new Point( 0,  1),
        new Point(-1,  0),
        new Point( 0, -1),
        new Point( 1,  1),
        new Point( 1, -1),
        new Point(-1,  1),
        new Point(-1, -1)
    };

    public WordSearch(string puzzle)
    {
        rows = puzzle.Split('\n');
        width = rows[0].Length;
        height = rows.Length;
    }

    public Tuple<Point, Point> Find(string word)
    {
        return
            Positions()
                .SelectMany(position => Directions.SelectMany(direction => Find(word, position, direction)))
                .FirstOrDefault();
    }

    private IEnumerable<Tuple<Point, Point>> Find(string word, Point position, Point direction)
    {
        var current = position;

        foreach (var letter in word)
        {
            if (FindChar(current) != letter)
            {
                yield break;
            }

            current.X += direction.X;
            current.Y += direction.Y;
        }

        yield return Tuple.Create(position, new Point(current.X - direction.X, current.Y - direction.Y));
    }

    private char? FindChar(Point coordinate)
    {
        if (coordinate.X > 0 && coordinate.X <= width && coordinate.Y > 0 && coordinate.Y <= height)
        {
            return rows[coordinate.Y - 1][coordinate.X - 1];
        }

        return null;
    }

    private IEnumerable<Point> Positions()
    {
        return Enumerable.Range(1, width).SelectMany(x =>
               Enumerable.Range(1, height).Select(y =>new Point(x, y)));
    }
}

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.8.14 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.13 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.12 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.11 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.10 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.9 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.8 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.7 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.6 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.5 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.4 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.3 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.2 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.8.1 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.7.0 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.6.44 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.6.43 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.6.42 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.6.41 tracks/csharp/exercises/word-search/Example.cs
trackler-2.0.6.40 tracks/csharp/exercises/word-search/Example.cs