Skip to content

Instantly share code, notes, and snippets.

@Kiso-blg
Created March 13, 2016 09:29

Revisions

  1. Kiso-blg created this gist Mar 13, 2016.
    100 changes: 100 additions & 0 deletions Problem_4___Text_Bombardment
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    using System;
    using System.IO;
    using System.Linq;

    namespace Problem_4___Text_Bombardment
    {
    class TextBombardment
    {
    static void Main(string[] args)
    {
    Stream inStream = Console.OpenStandardInput(2000);
    Console.SetIn(new StreamReader(inStream, Console.InputEncoding, false, 2000));

    string text = Console.ReadLine();
    int columns = int.Parse(Console.ReadLine());
    int[] targets = GetTargets();
    int rows = text.Length / columns;
    rows = text.Length % columns > 0 ? rows += 1 : rows;
    char[,] table = GetTable(rows, columns, text);
    BombardTable(targets, table);

    for (int row = 0; row < rows; row++)
    {
    for (int col = 0; col < columns; col++)
    {
    Console.Write(table[row, col]);
    }
    }

    Console.WriteLine();
    }

    private static void BombardTable(int[] targets, char[,] table)
    {
    bool destroy = true;

    for (int i = 0; i < targets.Length; i++)
    {
    int col = targets[i];

    for (int row = 0; row < table.GetLength(0); row++)
    {
    if (table[row, col] != ' ')
    {
    destroy = false;
    }

    if (table[row, col] != ' ')
    {
    table[row, col] = ' ';
    }
    else
    {
    if (!destroy)
    {
    break;
    }
    }
    }

    destroy = true;
    }
    }

    private static char[,] GetTable(int rows, int columns, string text)
    {
    char[,] table = new char[rows, columns];
    int idx = 0;

    for (int row = 0; row < rows; row++)
    {
    for (int col = 0; col < columns; col++)
    {
    if (idx < text.Length)
    {
    table[row, col] = text[idx];
    idx++;
    }
    else
    {
    table[row, col] = ' ';
    }
    }
    }

    return table;
    }

    private static int[] GetTargets()
    {
    int[] numbers = Console
    .ReadLine()
    .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(item => int.Parse(item))
    .ToArray();

    return numbers;
    }
    }
    }