Guest User

parking system task SoftUni

a guest
Jun 24th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ParkingSystem
  6. {
  7.     public class ParkingSystem
  8.     {
  9.         public static void Main()
  10.         {
  11.             int[] dimensions = Console.ReadLine()
  12.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.             int rows = dimensions[0];
  16.             int cols = dimensions[1];
  17.  
  18.             bool[,] parkingLot = new bool[rows, cols];
  19.  
  20.             string currentCommand = Console.ReadLine();
  21.             while (currentCommand != "stop")
  22.             {
  23.                 int[] commandTokens = currentCommand
  24.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  25.                     .Select(int.Parse)
  26.                     .ToArray();
  27.  
  28.                 int entryRow = commandTokens[0];
  29.                 int targetRow = commandTokens[1];
  30.                 int targetCol = commandTokens[2];
  31.  
  32.                 int distance = 0;
  33.                 bool rowIsFull = false;
  34.                 bool targetSpotIsFull = parkingLot[targetRow, targetCol];
  35.                 if (targetSpotIsFull)
  36.                 {
  37.                     // target spot is not free, find the nearest spot next to TARGETED and calculate the distance.
  38.                     int counter = 1;
  39.                     while (true)
  40.                     {
  41.                         // check for a free spot on the left of targeted
  42.                         int leftCol = targetCol - counter;
  43.  
  44.                         // check for a free spot on the right of targeted
  45.                         int rightCol = targetCol + counter;
  46.  
  47.                         // both left and right col indexes are out of matrix
  48.                         if (leftCol < 1 && rightCol >= parkingLot.GetLength(1))
  49.                         {
  50.                             Console.WriteLine($"Row {targetRow} full");
  51.                             rowIsFull = true;
  52.                             break;
  53.                         }
  54.                         // left col index is out, but right is still in
  55.                         else if (leftCol < 1 && rightCol < parkingLot.GetLength(1))
  56.                         {
  57.                             bool isRightCellFull = parkingLot[targetRow, rightCol];
  58.                             if (!isRightCellFull)
  59.                             {
  60.                                 distance = CalculateDistance(entryRow, targetRow, rightCol);
  61.                                 parkingLot[targetRow, rightCol] = true;
  62.                                 break;
  63.                             }
  64.                         }
  65.                         // right col index is out but left is still in
  66.                         else if (rightCol >= parkingLot.GetLength(1) && leftCol >= 1)
  67.                         {
  68.                             bool isLeftCellFull = parkingLot[targetRow, leftCol];
  69.                             if (!isLeftCellFull)
  70.                             {
  71.                                 distance = CalculateDistance(entryRow, targetRow, leftCol);
  72.                                 parkingLot[targetRow, leftCol] = true;
  73.                                 break;
  74.                             }
  75.                         }
  76.                         // both left and right col indexes are in the matrix
  77.                         else
  78.                         {
  79.                             bool isLeftCellFull = parkingLot[targetRow, leftCol];
  80.                             bool isRightCellFull = parkingLot[targetRow, rightCol];
  81.                             // if left spot is empty set it, calc distance and break
  82.                             if (!isLeftCellFull)
  83.                             {
  84.                                 parkingLot[targetRow, leftCol] = true;
  85.                                 distance = CalculateDistance(entryRow, targetRow, leftCol);
  86.                                 break;
  87.                             }
  88.                             // if left spot is not empty, but right is, set right spot, calc distance and break
  89.                             else if (!isRightCellFull)
  90.                             {
  91.                                 parkingLot[targetRow, rightCol] = true;
  92.                                 distance = CalculateDistance(entryRow, targetRow, rightCol);
  93.                                 break;
  94.                             }
  95.                             // if left spot is not empty and right spot is not empty - nothing
  96.                         }
  97.  
  98.                         counter++;
  99.                     }
  100.                 }
  101.                 else
  102.                 {
  103.                     // target spot is free calculate distance
  104.                     distance = CalculateDistance(entryRow, targetRow, targetCol);
  105.                     parkingLot[targetRow, targetCol] = true;
  106.                 }
  107.  
  108.  
  109.                 // Print distance
  110.                 if (!rowIsFull)
  111.                 {
  112.                     Console.WriteLine(distance);
  113.                 }
  114.  
  115.                 rowIsFull = false;
  116.                 currentCommand = Console.ReadLine();
  117.             }
  118.         }
  119.  
  120.         private static int CalculateDistance(int entryRow, int targetRow, int targetCol)
  121.         {
  122.             return Math.Abs(targetRow - entryRow) + targetCol + 1;
  123.         }
  124.     }
  125. }
Add Comment
Please, Sign In to add comment