Advertisement
Gesh4o

ParkingSystem 60/100

Feb 29th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. namespace _02.ParkingSystem
  2. {
  3. using System;
  4. using System.Linq;
  5.  
  6. public class ParkingSystemMain
  7. {
  8. public static void Main(string[] args)
  9. {
  10. int[] gridParameters =
  11. Console.ReadLine()
  12. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse)
  14. .ToArray();
  15.  
  16. int gridRows = gridParameters[0];
  17. int gridCols = gridParameters[1];
  18.  
  19. char[,] grid = new char[gridRows, gridCols];
  20.  
  21. string command = Console.ReadLine();
  22. while (command != "stop")
  23. {
  24. int[] commandInfo = command.Split(' ').Select(int.Parse).ToArray();
  25. int startingRow = commandInfo[0];
  26. int spotRow = commandInfo[1];
  27. int spotCol = commandInfo[2];
  28. string message = string.Empty;
  29.  
  30. if (grid[spotRow, spotCol] != 'c')
  31. {
  32. grid[spotRow, spotCol] = 'c';
  33. message = CalculateDistance(startingRow, spotRow, spotCol).ToString();
  34. }
  35. else
  36. {
  37. int indexCheck = 1;
  38. while (grid[spotRow, 0] != 'c')
  39. {
  40. if (spotCol - indexCheck >= 0)
  41. {
  42. int currentCol = spotCol - indexCheck;
  43. if (currentCol == 0)
  44. {
  45. message = string.Format($"Row {spotRow} full");
  46. break;
  47. }
  48. if (grid[spotRow, currentCol] != 'c')
  49. {
  50. grid[spotRow, currentCol] = 'c';
  51. message = CalculateDistance(startingRow, spotRow, currentCol).ToString();
  52. break;
  53. }
  54. }
  55.  
  56. if (spotCol + indexCheck < gridCols)
  57. {
  58. int currentCol = spotCol + indexCheck;
  59. if (grid[spotRow, currentCol] != 'c')
  60. {
  61. grid[spotRow, currentCol] = 'c';
  62. message = CalculateDistance(startingRow, spotRow, currentCol).ToString();
  63. break;
  64. }
  65. }
  66.  
  67. indexCheck++;
  68. }
  69. }
  70.  
  71. Console.WriteLine(message);
  72. command = Console.ReadLine();
  73. }
  74. }
  75.  
  76. private static int CalculateDistance(int startingRow, int spotRow, int spotCol)
  77. {
  78. int result = spotCol + 1;
  79. result += Math.Abs(startingRow - spotRow);
  80.  
  81. return result;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement