Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- namespace HackerRankProjects
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] inputStringArray = new string[6];
- inputStringArray[0] = ".......";
- inputStringArray[1] = "...O...";
- inputStringArray[2] = "....O..";
- inputStringArray[3] = ".......";
- inputStringArray[4] = "OO.....";
- inputStringArray[5] = "OO.....";
- string[] test = bomberMan(3, inputStringArray);
- Console.ReadKey();
- }
- const char BOMB = 'O';
- const string CLEAN = ".";
- // Complete the bomberMan function below.
- static string[] bomberMan(int n, string[] grid)
- {
- var stopTimer = Stopwatch.StartNew();
- var columnSize = grid[0].Length;
- bool explodeTime = false;
- string[] checkpoint = new string[grid.Length];
- string[] gridOriginal = new string[grid.Length];
- grid.CopyTo(gridOriginal, 0);
- grid.CopyTo(checkpoint, 0);
- if (n % 2 == 0)
- {
- PlantAll(grid, columnSize);
- return grid;
- }
- int numberCycles = 0;
- n = n % 32;
- for (int secs = 2; secs <= n; secs++)
- {
- if (explodeTime)
- {
- for (int line = 0; line < grid.Length; line++)
- {
- for (int column = 0; column < columnSize; column++)
- {
- var itsGonnaExplode = checkpoint[line][column] == BOMB;
- if (itsGonnaExplode)
- ExplodeArea(grid, line, column, columnSize, checkpoint);
- }
- }
- grid.CopyTo(checkpoint, 0);
- }
- else
- {
- PlantAll(grid, columnSize);
- }
- numberCycles++;
- if (Enumerable.SequenceEqual(gridOriginal, grid))
- {
- n = n % numberCycles;
- secs = 2;
- }
- explodeTime = !explodeTime;
- }
- stopTimer.Stop();
- Console.WriteLine(stopTimer.ElapsedMilliseconds);
- return grid;
- }
- private static void ExplodeArea(string[] grid, int line, int column, int columnSize, string[] checkpoint)
- {
- grid[line] = grid[line].Remove(column, 1).Insert(column, CLEAN);
- //Has plant for both sides.
- if (column > 0 && column < columnSize - 1)
- {
- if (checkpoint[line][column - 1] != BOMB)
- grid[line] = grid[line].Remove(column - 1, 1).Insert(column - 1, CLEAN);
- if (checkpoint[line][column + 1] != BOMB)
- grid[line] = grid[line].Remove(column + 1, 1).Insert(column + 1, CLEAN);
- }
- else if (column > 0)
- {
- if (checkpoint[line][column - 1] != BOMB)
- grid[line] = grid[line].Remove(column - 1, 1).Insert(column - 1, CLEAN);
- }
- else if (column < columnSize - 1)
- {
- if (checkpoint[line][column + 1] != BOMB)
- grid[line] = grid[line].Remove(column + 1, 1).Insert(column + 1, CLEAN);
- }
- //Has plant for up and down
- if (line > 0 && line < grid.Length - 1)
- {
- if (checkpoint[line - 1][column] != BOMB)
- grid[line - 1] = grid[line - 1].Remove(column, 1).Insert(column, CLEAN);
- if (checkpoint[line + 1][column] != BOMB)
- grid[line + 1] = grid[line + 1].Remove(column, 1).Insert(column, CLEAN);
- }
- else if (line > 0)
- {
- if (checkpoint[line - 1][column] != BOMB)
- grid[line - 1] = grid[line - 1].Remove(column, 1).Insert(column, CLEAN);
- }
- else if (line < grid.Length - 1)
- {
- if (checkpoint[line + 1][column] != BOMB)
- grid[line + 1] = grid[line + 1].Remove(column, 1).Insert(column, CLEAN);
- }
- }
- private static void PlantAll(string[] grid, int lenght)
- {
- for (int i = 0; i < grid.Length; i++)
- grid[i] = new string('O', lenght);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement