Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. namespace _04PRoblem
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Runtime.CompilerServices;
  7.     using System.Text.RegularExpressions;
  8.  
  9.     public class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             List<Region> regions=new List<Region>();
  14.             string pattern = @"Grow <([A-Z]{1}[a-z]+)> <([a-zA-Z0-9]+)> ([0-9]+)";
  15.             Regex regex=new Regex(pattern);
  16.             while (true)
  17.             {
  18.                 var inputLine = Console.ReadLine();
  19.                 if (inputLine == "Icarus, Ignite!")
  20.                 {
  21.                     break;
  22.                 }
  23.  
  24.                 Match match = regex.Match(inputLine);
  25.                 if (match.Groups.Count ==4)
  26.                 {
  27.                     string regionName = match.Groups[1].Value;
  28.                     string flowerName = match.Groups[2].Value;
  29.                     long amount = long.Parse(match.Groups[3].Value);
  30.                     if (amount<=int.MaxValue && amount>0) {
  31.                     var existRegion = regions.FindIndex(r => r.Name == regionName);
  32.                     if (existRegion == -1)
  33.                     {
  34.                         regions.Add(new Region(regionName));
  35.                         var currentRegion = regions.First(r => r.Name == regionName);
  36.                         var newFlower = new Flower(flowerName, amount);
  37.                         currentRegion.Flowers.Add(newFlower);
  38.                         currentRegion.TotalFlowers += amount;
  39.  
  40.                     }
  41.                     else
  42.                     {
  43.                         var currentRegion = regions.First(r => r.Name == regionName);
  44.                         var existFlower = currentRegion.Flowers.FindIndex(f => f.Name == flowerName);
  45.                         if (existFlower == -1)
  46.                         {
  47.                             var newFlower = new Flower(flowerName, amount);
  48.                             currentRegion.Flowers.Add(newFlower);
  49.                             currentRegion.TotalFlowers += amount;
  50.                         }
  51.                         else
  52.                         {
  53.                             var currentFlower = currentRegion.Flowers.First(f => f.Name == flowerName);
  54.                             currentFlower.Amount += amount;
  55.                             currentRegion.TotalFlowers += amount;
  56.                         }
  57.                     }
  58.                 }
  59.                 }
  60.             }
  61.             var newList = regions.OrderByDescending(r => r.TotalFlowers).ThenBy(r => r.Name).ToList();
  62.             foreach (var region in newList)
  63.             {
  64.                 var sortedFlowers = region.Flowers.OrderBy(f => f.Amount).ThenBy(f => f.Name);
  65.                 Console.WriteLine(region.Name);
  66.                 foreach (var flower in sortedFlowers)
  67.                 {
  68.                     Console.WriteLine("*--{0} | {1}",flower.Name,flower.Amount);
  69.                 }
  70.             }
  71.            
  72.         }
  73.     }
  74.  
  75.     public class Region
  76.     {
  77.         public Region(string name)
  78.         {
  79.             this.Name = name;
  80.             this.Flowers=new List<Flower>();
  81.         }
  82.         public string Name { get; set; }
  83.         public List<Flower> Flowers { get; set; }
  84.         public long TotalFlowers { get; set; }
  85.  
  86.     }
  87.  
  88.     public class Flower
  89.     {
  90.         public Flower(string name,long amount)
  91.         {
  92.             this.Name = name;
  93.             this.Amount = amount;
  94.         }
  95.         public string Name { get; set; }
  96.         public long Amount { get; set; }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement