Advertisement
Guest User

Parking System

a guest
Mar 1st, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. namespace ParkingSystem
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.  
  9.     public class ParkingSystemMain
  10.     {
  11.         static bool[,] parkingLot;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             var parkingSize = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
  16.             parkingLot = new bool[parkingSize.Last(), parkingSize.First()];
  17.  
  18.             string carInitialData = Console.ReadLine();
  19.             while (carInitialData != "stop")
  20.             {
  21.                 var carDataArr = carInitialData.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
  22.                 int entryRow = carDataArr.ElementAt(0);
  23.                 int desiredCol = carDataArr.ElementAt(2);
  24.                 int desiredRow = carDataArr.ElementAt(1);
  25.  
  26.                 int stepsCount = (desiredRow - entryRow) >= 0 ? (desiredRow - entryRow) + 1 : (entryRow - desiredRow) + 1;
  27.  
  28.  
  29.                 int leftPosition = 0;
  30.                 int rightPosition = 0;
  31.                
  32.                 if (!parkingLot[desiredCol, desiredRow])
  33.                 {
  34.                     parkingLot[desiredCol, desiredRow] = true;
  35.                     stepsCount += desiredCol;
  36.                     Console.WriteLine(stepsCount);
  37.                     carInitialData = Console.ReadLine();
  38.                     continue;
  39.                 }
  40.                 else
  41.                 {
  42.                     leftPosition = CheckLeft(desiredRow, desiredCol);
  43.                     rightPosition = CheckRigth(desiredRow, desiredCol);
  44.                 }
  45.  
  46.                 if (leftPosition <= 0 && rightPosition < 0)
  47.                 {
  48.                     Console.WriteLine(string.Format("Row {0} full", desiredRow));
  49.                     carInitialData = Console.ReadLine();
  50.                     continue;
  51.                 }
  52.                 else if (leftPosition <= 0)
  53.                 {
  54.                     stepsCount += rightPosition;
  55.                     parkingLot[rightPosition, desiredRow] = true;
  56.                 }
  57.                 else if (rightPosition < 0)
  58.                 {
  59.                     stepsCount += leftPosition;
  60.                     parkingLot[leftPosition, desiredRow] = true;
  61.                 }
  62.                 else
  63.                 {
  64.                     if (Math.Abs(leftPosition - desiredCol) <= Math.Abs(rightPosition - desiredCol))
  65.                     {
  66.                         stepsCount += leftPosition;
  67.                         parkingLot[leftPosition, desiredRow] = true;
  68.                     }
  69.                     else
  70.                     {
  71.                         stepsCount += rightPosition;
  72.                         parkingLot[rightPosition, desiredRow] = true;
  73.                     }
  74.                 }
  75.  
  76.                 Console.WriteLine(stepsCount);
  77.  
  78.                 carInitialData = Console.ReadLine();
  79.             }
  80.         }
  81.  
  82.         private static int CheckRigth(int desiredRow, int desiredCol)
  83.         {
  84.             int resultIfAllBusy = -1;
  85.             for (int i = desiredCol + 1; i < parkingLot.GetLength(0); i++)
  86.             {
  87.                 if (!parkingLot[i, desiredRow])
  88.                 {
  89.                     return i;
  90.                 }
  91.             }
  92.  
  93.             return resultIfAllBusy;
  94.         }
  95.  
  96.         private static int CheckLeft(int desiredRow, int desiredCol)
  97.         {
  98.             int resultIfAllBusy = 0;
  99.             for (int i = desiredCol ; i >= 0; i--)
  100.             {
  101.                 if (!parkingLot[i, desiredRow])
  102.                 {
  103.                     return i;
  104.                 }
  105.             }
  106.  
  107.             return resultIfAllBusy;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement