Advertisement
Guest User

Srabsko Unleashed

a guest
Feb 27th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. namespace SrabskoUnleashed
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Text.RegularExpressions;
  8.     using System.Threading.Tasks;
  9.  
  10.     class Program
  11.     {
  12.         const string pattern = @"([a-zA-Z]+\s){1,3}@([a-zA-Z0-9]+\s){1,3}[0-9]+\s[0-9]+";
  13.  
  14.         static void Main()
  15.         {
  16.             Dictionary<string, List<Singer>> venues = new Dictionary<string, List<Singer>>();
  17.             string input = Console.ReadLine();
  18.  
  19.             while (input != "End")
  20.             {
  21.                 var matches = Regex.Matches(input, pattern);
  22.  
  23.                 if (matches.Count > 0)
  24.                 {
  25.                     string line = matches[0].Value;
  26.                     string[] split = line.Split('@');
  27.                     string name = split[0].Trim();
  28.                     string[] secondSplit = split[1].Split(' ');
  29.                     int ticketsCount = int.Parse(secondSplit.Last());
  30.                     int ticketsPrice = int.Parse(secondSplit[secondSplit.Length - 2]);
  31.                     string place = secondSplit[0];
  32.  
  33.                     for (int i = 1; i < secondSplit.Length - 2; i++)
  34.                     {
  35.                         place += " " + secondSplit[i];
  36.                     }
  37.  
  38.                     Singer singer = new Singer(name, ticketsCount * ticketsPrice);
  39.  
  40.                     if (!venues.ContainsKey(place))
  41.                     {
  42.                         venues.Add(place, new List<Singer>());
  43.                     }
  44.  
  45.                     if (venues[place].Contains(singer))
  46.                     {
  47.                         var contained = venues[place].First(s => s.Name == singer.Name);
  48.                         contained.TotalMoney += singer.TotalMoney;
  49.                     }
  50.                     else
  51.                     {
  52.                         venues[place].Add(singer);
  53.                     }
  54.                 }
  55.  
  56.                 input = Console.ReadLine();
  57.             }
  58.  
  59.             foreach (var pair in venues)
  60.             {
  61.                 pair.Value.Sort();
  62.                 Console.WriteLine(pair.Key);
  63.  
  64.                 foreach (var singer in pair.Value)
  65.                 {
  66.                     Console.WriteLine("#  {0} -> {1}", singer.Name, singer.TotalMoney);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     class Singer : IComparable
  73.     {
  74.         public string Name { get; set; }
  75.  
  76.         public int TotalMoney { get; set; }
  77.  
  78.         public Singer(string name, int money)
  79.         {
  80.             this.Name = name;
  81.             this.TotalMoney = money;
  82.         }
  83.  
  84.         public int CompareTo(object obj)
  85.         {
  86.             return -(this.TotalMoney.CompareTo((obj as Singer).TotalMoney));
  87.         }
  88.  
  89.         public override bool Equals(object obj)
  90.         {
  91.             return this.Name.Equals((obj as Singer).Name);
  92.         }
  93.  
  94.         public override int GetHashCode()
  95.         {
  96.             return this.Name.GetHashCode();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement