Skip to content

Instantly share code, notes, and snippets.

@Kiso-blg
Created March 13, 2016 09:29
Show Gist options
  • Save Kiso-blg/1a8a8946523193a3bbcf to your computer and use it in GitHub Desktop.
Save Kiso-blg/1a8a8946523193a3bbcf to your computer and use it in GitHub Desktop.
Basics Exam 20 December 2014
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment