WindFell

Grains of Sand

Aug 28th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class GrainsOfSand
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         List<int> sands = Console.ReadLine()
  10.             .Split()
  11.             .Select(int.Parse)
  12.             .ToList();
  13.  
  14.         string input;
  15.  
  16.         while ((input = Console.ReadLine()) != "Mort")
  17.         {
  18.             string[] commandArgs = input.Split();
  19.             string command = commandArgs[0];
  20.             int value = int.Parse(commandArgs[1]);
  21.             int index = sands.IndexOf(value);
  22.  
  23.             switch (command)
  24.             {
  25.                 case "Add":
  26.                     sands = Add(sands, value);
  27.                     break;
  28.                 case "Remove":
  29.                     sands = Remove(sands, value, index);
  30.                     break;
  31.                 case "Replace":
  32.                     int replacement = int.Parse(commandArgs[2]);
  33.                     sands = Replace(sands, value, replacement, index);
  34.                     break;
  35.                 case "Increase":
  36.                     sands = Increase(sands, value, index);
  37.                     break;
  38.                 case "Collapse":
  39.                     sands = Collapse(sands, value);
  40.                     break;
  41.             }
  42.         }
  43.  
  44.         Print(sands);
  45.     }
  46.  
  47.     private static void Print(List<int> sands)
  48.     {
  49.         string result = string.Join(' ', sands);
  50.         Console.WriteLine(result);
  51.     }
  52.  
  53.     private static List<int> Add(List<int> sands, int value)
  54.     {
  55.         sands.Add(value);
  56.         return sands;
  57.     }
  58.  
  59.     private static List<int> Remove(List<int> sands, int value, int index)
  60.     {
  61.         if (index != -1)
  62.         {
  63.             sands.RemoveAt(index);
  64.         }
  65.         else
  66.         {
  67.             if (0 <= value && value < sands.Count)
  68.             {
  69.                 sands.RemoveAt(value);
  70.             }
  71.         }
  72.  
  73.         return sands;
  74.     }
  75.  
  76.     private static List<int> Replace(List<int> sands, int value, int replacement, int index)
  77.     {
  78.         if (index != -1)
  79.         {
  80.             sands[index] = replacement;
  81.         }
  82.  
  83.         return sands;
  84.     }
  85.  
  86.     private static List<int> Increase(List<int> sands, int value, int index)
  87.     {
  88.         if (index == -1)
  89.         {
  90.             value = sands.Last();
  91.         }
  92.  
  93.         for (int elIndex = 0; elIndex < sands.Count; elIndex++)
  94.         {
  95.             sands[elIndex] += value;
  96.         }
  97.  
  98.         return sands;
  99.     }
  100.  
  101.     private static List<int> Collapse(List<int> sands, int value)
  102.     {
  103.         sands.RemoveAll(e => e < value);
  104.  
  105.         return sands;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment