Created
September 4, 2016 09:38
-
-
Save vdonchev/084c02c1598b9d47800d97c792a5581e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace _01.SecondNatureV2 | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Program | |
{ | |
private static List<int> secondNatureFlowers = new List<int>(); | |
public static void Main() | |
{ | |
var flowers = Console.ReadLine() | |
.Split(' ') | |
.Select(int.Parse) | |
.ToList(); | |
var buckets = new Stack<int>(Console.ReadLine() | |
.Split(' ') | |
.Select(int.Parse)); | |
var flowerIndex = 0; | |
while (buckets.Any() && flowerIndex < flowers.Count) | |
{ | |
var currentBucket = buckets.Peek(); | |
var currentFlower = flowers[flowerIndex]; | |
if (currentBucket == currentFlower) | |
{ | |
secondNatureFlowers.Add(currentFlower); | |
buckets.Pop(); | |
flowerIndex++; | |
} | |
else | |
{ | |
flowers[flowerIndex] -= currentBucket; | |
if (flowers[flowerIndex] <= 0) | |
{ | |
flowers.RemoveAt(flowerIndex); | |
var remainingWater = buckets.Pop() - currentFlower; | |
if (buckets.Any()) | |
{ | |
remainingWater += buckets.Pop(); | |
} | |
buckets.Push(remainingWater); | |
} | |
else | |
{ | |
buckets.Pop(); | |
} | |
} | |
} | |
Console.WriteLine(buckets.Any()? string.Join(" ", buckets) : string.Join(" ", flowers)); | |
Console.WriteLine(secondNatureFlowers.Any() ? string.Join(" ", secondNatureFlowers) : "None"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment