Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _02GrainsOfSands
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> sands = Console.ReadLine().Split().Select(int.Parse).ToList();
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "Mort")
- {
- break;
- }
- string[] tokens = command.Split();
- string action = tokens[0];
- if (action == "Add")
- {
- int value = int.Parse(tokens[1]);
- sands.Add(value);
- }
- else if (action == "Remove")
- {
- int value = int.Parse(tokens[1]);
- if (sands.Contains(value))
- {
- sands.Remove(value);
- }
- else
- {
- int index = value;
- if (index>=0 && index<=sands.Count-1)
- {
- sands.RemoveAt(index);
- }
- }
- }
- else if (action == "Replace")
- {
- int value = int.Parse(tokens[1]);
- int replacement = int.Parse(tokens[2]);
- for (int i = 0; i < sands.Count; i++)
- {
- int currNum = sands[i];
- if (currNum == value)
- {
- sands[i] = replacement;
- break;
- }
- }
- }
- else if (action == "Increase")
- {
- int value = int.Parse(tokens[1]);
- bool isFound = false;
- for (int i = 0; i < sands.Count; i++)
- {
- int currNum = sands[i];
- if (currNum>=value)
- {
- isFound = true;
- for (int j = 0; j < sands.Count; j++)
- {
- sands[j] += currNum;
- }
- break;
- }
- }
- if (isFound == false)
- {
- int valueToIncrease = sands[sands.Count - 1];
- for (int i = 0; i < sands.Count; i++)
- {
- sands[i] += valueToIncrease;
- }
- }
- }
- else if (action == "Collapse")
- {
- int value = int.Parse(tokens[1]);
- sands = sands.Where(x => x >= value).ToList();
- }
- }
- Console.WriteLine(string.Join(" ",sands));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment