Advertisement
simonradev

SecondNature

Apr 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. namespace _01.SecondNature
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.    
  8.     public class Startup
  9.     {
  10.         public static List<int> secondNatureFlowers;
  11.         public static Queue<int> flowers;
  12.         public static Stack<int> buckets;
  13.  
  14.         public static void Main()
  15.         {
  16.             secondNatureFlowers = new List<int>();
  17.  
  18.             flowers = new Queue<int>(ReadSequenceFromConsole());
  19.             buckets = new Stack<int>(ReadSequenceFromConsole());
  20.  
  21.             while (CheckIfCollectionsAreEmpty())
  22.             {
  23.                 int currFlower = flowers.Dequeue();
  24.                 int currBucket = buckets.Pop();
  25.  
  26.                 if (currFlower == currBucket)
  27.                 {
  28.                     secondNatureFlowers.Add(currFlower);
  29.                 }
  30.                 else if (currFlower > currBucket)
  31.                 {
  32.                     currFlower -= currBucket;
  33.                     flowers.Enqueue(currFlower);
  34.                 }
  35.                 else if (currFlower < currBucket)
  36.                 {
  37.                     int remainingWater = currBucket - currFlower;
  38.  
  39.                     int waterToAdd = 0;
  40.                     if (buckets.Count == 0)
  41.                     {
  42.                         waterToAdd = remainingWater;
  43.                     }
  44.                     else
  45.                     {
  46.                         waterToAdd = buckets.Pop() + remainingWater;
  47.                     }
  48.  
  49.                     buckets.Push(waterToAdd);
  50.                 }
  51.             }
  52.  
  53.             string leftOvers = flowers.Count != 0 ?
  54.                                 JoinString(flowers) :
  55.                                 JoinString(buckets);
  56.             string specialFlowersResult = secondNatureFlowers.Count != 0 ?
  57.                                             JoinString(secondNatureFlowers) :
  58.                                             "None";
  59.  
  60.             StringBuilder result = new StringBuilder();
  61.             result.AppendLine(leftOvers);
  62.             result.AppendLine(specialFlowersResult);
  63.  
  64.             Console.Write(result);
  65.         }
  66.  
  67.         private static string JoinString(IEnumerable<int> toJoin)
  68.         {
  69.             return string.Join(" ", toJoin);
  70.         }
  71.  
  72.         private static bool CheckIfCollectionsAreEmpty()
  73.         {
  74.             return flowers.Count > 0 && buckets.Count > 0;
  75.         }
  76.  
  77.         private static IEnumerable<int> ReadSequenceFromConsole()
  78.         {
  79.             return Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement