Example #1
0
    public static void Can_Parse_Characters()
    {
        // Arrange
        string[] letters =
        {
            ".**..***...**..****.****..**..*..*..***...**.*..*.*.....**..***..***..*..*.*...*****.",
            "*..*.*..*.*..*.*....*....*..*.*..*...*.....*.*.*..*....*..*.*..*.*..*.*..*.*...*...*.",
            "*..*.***..*....***..***..*....****...*.....*.**...*....*..*.*..*.*..*.*..*..*.*...*..",
            "****.*..*.*....*....*....*.**.*..*...*.....*.*.*..*....*..*.***..***..*..*...*...*...",
            "*..*.*..*.*..*.*....*....*..*.*..*...*..*..*.*.*..*....*..*.*....*.*..*..*...*..*....",
            "*..*.***...**..****.*.....***.*..*..***..**..*..*.****..**..*....*..*..**....*..****.",
        };

        // Act
        string actual = CharacterRecognition.Read(string.Join(Environment.NewLine, letters));

        // Assert
        actual.ShouldBe("ABCEFGHIJKLOPRUYZ");
    }
Example #2
0
    /// <summary>
    /// Gets the number of pixels lit in the specified grid after following the specified instructions.
    /// </summary>
    /// <param name="instructions">The instructions to use to manipulate the grid of lights.</param>
    /// <param name="width">The width of the grid.</param>
    /// <param name="height">The height of the grid.</param>
    /// <param name="logger">The logger to use.</param>
    /// <returns>
    /// The number of pixels lit in the grid once the instructions are processed,
    /// the code displayed, and a visualization.
    /// </returns>
    internal static (int PixelsLit, string Code, string Visualization) GetPixelsLit(
        IEnumerable <string> instructions,
        int width,
        int height,
        ILogger logger)
    {
        var operations = instructions
                         .Select((p) => ParseInstruction(p))
                         .ToArray();

        bool[,] grid = new bool[width, height];

        foreach (Instruction instruction in operations)
        {
            if (instruction.IsRotation)
            {
                if (instruction.IsColumn)
                {
                    RotateColumn(grid, column: instruction.A, pixels: instruction.B);
                }
                else
                {
                    RotateRow(grid, row: instruction.A, pixels: instruction.B);
                }
            }
            else
            {
                LightRectangle(grid, width: instruction.A, height: instruction.B);
            }
        }

        int pixelsLit = CountLitPixels(grid);

        string code          = CharacterRecognition.Read(grid);
        string visualization = logger.WriteGrid(grid, ' ', 'X');

        return(pixelsLit, code, visualization);
    }