emilia98

Ticket_Trouble

Oct 12th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _00.TRY
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string input = Console.ReadLine();
  12.             string text = Console.ReadLine();
  13.  
  14.             var locationDetails = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  15.             string countryCode = locationDetails[0];
  16.             string cityCode = locationDetails[1];
  17.  
  18.             var pattern = new Regex(@"(?<curly>\{[^\{\}]*?(\[(?<dest>[A-Z]{3}\s*[A-Z]{2})\])[^\{\}]*?(\[(?<seats>[A-Z]{1}\s*[0-9]{1,2})\])[^\{\}]*?\})|(?<square>\[[^\[\]]*?(\{(?<dest2>[A-Z]{3}\s*[A-Z]{2})\})[^\[\]]*?(\{(?<seats2>[A-Z]{1}\s*[0-9]{1,2})\})[^\[\]]*?\])");
  19.  
  20.             MatchCollection matches = pattern.Matches(text);
  21.             var seatsData = new Dictionary<int, List<string>>();
  22.             var seats = new List<string>();
  23.  
  24.             foreach (Match match in matches)
  25.             {
  26.                 var groups = match.Groups;
  27.                 string destination = null;
  28.                 string seat = null;
  29.  
  30.  
  31.                 if (groups["curly"].Success)
  32.                 {
  33.                     destination = groups["dest"].Value;
  34.                     seat = groups["seats"].Value;
  35.                 }
  36.                 else if (groups["square"].Success)
  37.                 {
  38.                     destination = groups["dest2"].Value;
  39.                     seat = groups["seats2"].Value;
  40.                 }
  41.  
  42.                 var tokens_dest = destination.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  43.                 string toCountry = tokens_dest[0];
  44.                 string toCity = tokens_dest[1];
  45.  
  46.                 int seat_row = int.Parse(seat.Substring(1));
  47.                 char seat_col = seat[0];
  48.  
  49.                 if (toCountry == countryCode && toCity == cityCode)
  50.                 {
  51.                     seats.Add(seat);
  52.                     if (seatsData.ContainsKey(seat_row) == false)
  53.                     {
  54.                         seatsData.Add(seat_row, new List<string>());
  55.                     }
  56.  
  57.                     seatsData[seat_row].Add(seat);
  58.                 }
  59.             }
  60.             if (seats.Count == 2)
  61.             {
  62.                 Console.WriteLine($"You are traveling to {countryCode} {cityCode} on seats {seats[0]} and {seats[1]}.");
  63.                 return;
  64.             }
  65.  
  66.             foreach (var seatsRow in seatsData)
  67.             {
  68.                 var seatsOnThisRow = seatsRow.Value;
  69.                 int seatsCount = seatsOnThisRow.Count;
  70.  
  71.                 if (seatsCount >= 2)
  72.                 {
  73.                     Console.WriteLine($"You are traveling to {countryCode} {cityCode} on seats {seatsOnThisRow[0]} and {seatsOnThisRow[1]}.");
  74.                     return;
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment