Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SecondNature
- 2
- {
- 3
- using System;
- 4
- using System.Collections.Generic;
- 5
- using System.Linq;
- 6
- using System.Text;
- 7
- using System.Threading.Tasks;
- 8
- 9
- public class StartUp
- 10
- {
- 11
- public static void Main()
- 12
- {
- 13
- Stack<int> edelFlowers = new Stack<int>(ReadSequenceFromTheConsole().Reverse());
- 14
- Stack<int> buckets = new Stack<int>(ReadSequenceFromTheConsole());
- 15
- 16
- Queue<int> secondNatureFlowers = new Queue<int>();
- 17
- 18
- while (CollectionIsNotEmpty(edelFlowers) && CollectionIsNotEmpty(buckets))
- 19
- {
- 20
- int currentFlower = edelFlowers.Peek();
- 21
- int currentBucket = buckets.Peek();
- 22
- 23
- int weissDust = currentFlower - currentBucket;
- 24
- 25
- if (weissDust == 0)
- 26
- {
- 27
- //Add the second nature flower by removing it from the others.
- 28
- secondNatureFlowers.Enqueue(edelFlowers.Pop());
- 29
- 30
- //Remove the bucket of water
- 31
- buckets.Pop();
- 32
- }
- 33
- else if (weissDust > 0)
- 34
- {
- 35
- //not enough water
- 36
- //replace the current flower with its decreased value
- 37
- edelFlowers.Pop();
- 38
- edelFlowers.Push(weissDust);
- 39
- 40
- //get the new bucket
- 41
- buckets.Pop();
- 42
- }
- 43
- else if (weissDust < 0)
- 44
- {
- 45
- //too much water
- 46
- //remove the current flower because it bloomed
- 47
- edelFlowers.Pop();
- 48
- 49
- //remove the current bucket
- 50
- buckets.Pop();
- 51
- 52
- int leftOverWeissDust = Math.Abs(weissDust);
- 53
- //add the remaining water to the next flower
- 54
- if (buckets.Count == 0)
- 55
- {
- 56
- buckets.Push(leftOverWeissDust);
- 57
- }
- 58
- else
- 59
- {
- 60
- buckets.Push(buckets.Pop() + leftOverWeissDust);
- 61
- }
- 62
- }
- 63
- }
- 64
- 65
- StringBuilder result = new StringBuilder();
- 66
- if (buckets.Count > 0)
- 67
- {
- 68
- result.AppendLine(JoinCollectionWithSpace(buckets));
- 69
- }
- 70
- else
- 71
- {
- 72
- result.AppendLine(JoinCollectionWithSpace(edelFlowers));
- 73
- }
- 74
- 75
- if (secondNatureFlowers.Count > 0)
- 76
- {
- 77
- result.AppendLine(JoinCollectionWithSpace(secondNatureFlowers));
- 78
- }
- 79
- else
- 80
- {
- 81
- result.AppendLine("None");
- 82
- }
- 83
- 84
- Console.Write(result);
- 85
- }
- 86
- 87
- private static bool CollectionIsNotEmpty(Stack<int> collection)
- 88
- {
- 89
- return collection.Count > 0;
- 90
- }
- 91
- 92
- private static string JoinCollectionWithSpace(IEnumerable<int> collection)
- 93
- {
- 94
- return string.Join(" ", collection);
- 95
- }
- 96
- 97
- private static IEnumerable<int> ReadSequenceFromTheConsole()
- 98
- {
- 99
- return Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
- 100
- }
- 101
- }
- 102
- }
- 103
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement