Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Created September 4, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdonchev/084c02c1598b9d47800d97c792a5581e to your computer and use it in GitHub Desktop.
Save vdonchev/084c02c1598b9d47800d97c792a5581e to your computer and use it in GitHub Desktop.
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