Aliendreamer

matrix parking lot

Sep 24th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.     public class TheParkingSystem
  6.     {
  7.         public static void Main()
  8.         {
  9.             var parking = new Dictionary<int, HashSet<int>>();
  10.             var size = Console.ReadLine()
  11.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.             var cols = size[1];
  15.  
  16.             while (true)
  17.             {
  18.                 string input = Console.ReadLine().Trim();
  19.  
  20.                 if (input == "stop")
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 var args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  26.                     .Select(int.Parse)
  27.                     .ToArray();
  28.                 var entryRow = args[0];
  29.                 var targetRow = args[1];
  30.                 var targetCol = args[2];
  31.  
  32.                 if (!IsOccupiedSpot(parking, targetRow, targetCol))
  33.                 {
  34.                     TakeParkingSpot(parking, targetRow, targetCol);
  35.                     Console.WriteLine(CalcDistanceToParkingSpot(entryRow, targetRow, targetCol));
  36.                 }
  37.                 else
  38.                 {
  39.                     targetCol = TryFindEmptySpot(parking[targetRow], targetCol, cols);
  40.                     if (targetCol == 0)
  41.                     {
  42.                         Console.WriteLine($"Row {targetRow} full");
  43.                     }
  44.                     else
  45.                     {
  46.                         TakeParkingSpot(parking, targetRow, targetCol);
  47.                         Console.WriteLine(CalcDistanceToParkingSpot(entryRow, targetRow, targetCol));
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.  
  53.         private static int TryFindEmptySpot(HashSet<int> parkingRow, int targetCol, int cols)
  54.         {
  55.             var targetColFound = 0;
  56.             int minDistance = int.MaxValue;
  57.  
  58.             if (parkingRow.Count == cols - 1)
  59.             {
  60.                 return targetColFound;
  61.             }
  62.  
  63.             for (int col = 1; col < cols; col++)
  64.             {
  65.                 var currentDistance = Math.Abs(targetCol - col);
  66.  
  67.                 if (!parkingRow.Contains(col) && currentDistance < minDistance)
  68.                 {
  69.                     targetColFound = col;
  70.                     minDistance = currentDistance;
  71.                 }
  72.             }
  73.  
  74.             return targetColFound;
  75.         }
  76.  
  77.         private static long CalcDistanceToParkingSpot(int entryRow, int targetRow, int targetCol)
  78.         {
  79.             return Math.Abs(targetRow - entryRow) + targetCol + 1;
  80.         }
  81.  
  82.         private static void TakeParkingSpot(Dictionary<int, HashSet<int>> parking, int row, int col)
  83.         {
  84.             if (!parking.ContainsKey(row))
  85.             {
  86.                 parking[row] = new HashSet<int>();
  87.             }
  88.  
  89.             parking[row].Add(col);
  90.         }
  91.  
  92.         private static bool IsOccupiedSpot(Dictionary<int, HashSet<int>> parking, int row, int col)
  93.         {
  94.             return parking.ContainsKey(row) && parking[row].Contains(col);
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment