Skip to content

Instantly share code, notes, and snippets.

@DimitrovZ
Created May 26, 2015 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DimitrovZ/a9554147b19f1da552de to your computer and use it in GitHub Desktop.
Save DimitrovZ/a9554147b19f1da552de to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextGravity
{
class TextGravity
{
static void Main(string[] args)
{
int length = int.Parse(Console.ReadLine());
string inputLine = Console.ReadLine();
int arrayCount = inputLine.Length / length;
if(inputLine.Length % length != 0)
{
arrayCount += 1;
}
char[,] matrix = new char[arrayCount,length];
int counter = 0;
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
if(counter == inputLine.Length)
{
matrix[row, col] = ' ';
continue;
}
else
{
matrix[row, col] = (char)inputLine[counter];
counter++;
}
}
}
bool aCharFellDown = true;
while(aCharFellDown)
{
aCharFellDown = false;
for (int row = 0; row < matrix.GetLength(0) - 1; row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
if(matrix[row,col]!= ' ' && matrix[row + 1,col] == ' ')
{
char temp = matrix[row + 1,col];
matrix[row + 1, col] = matrix[row, col];
matrix[row, col] = temp;
aCharFellDown = true;
}
}
}
}
for (int r = 0; r < matrix.GetLength(0); r++)
{
if(r == 0)
{
Console.Write("<table>");
}
Console.Write("<tr>");
for (int c = 0; c <matrix.GetLength(1); c++)
{
Console.Write("<td>"+matrix[r,c]+"</td>");
}
Console.Write("</tr>");
}
Console.WriteLine("</table>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment