Advertisement
markovood

Inferno III

Aug 6th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace InfernoIII_Variant
  6. {
  7.     public class InfernoIII_Variant
  8.     {
  9.         public static void Main()
  10.         {
  11.             Action<List<int>> printAction = list => Console.WriteLine(string.Join(' ', list));
  12.  
  13.             List<int> sequence = Console.ReadLine()
  14.                                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  15.                                     .Select(int.Parse)
  16.                                     .ToList();
  17.  
  18.             List<string> commands = new List<string>();
  19.  
  20.             // here we collect all the commands to execute after Forge
  21.             while (true)
  22.             {
  23.                 string command = Console.ReadLine();
  24.                 if (command == "Forge")
  25.                 {
  26.                     break;
  27.                 }
  28.  
  29.                 // command;filter type;filter parameter
  30.                 string[] commandArgs = command.Split(';', StringSplitOptions.RemoveEmptyEntries);
  31.                 string operation = commandArgs[0];
  32.                 string filterType = commandArgs[1];
  33.                 string filterParameter = commandArgs[2];
  34.  
  35.                 switch (operation)
  36.                 {
  37.                     case "Exclude":
  38.                         commands.Add(filterType + ";" + filterParameter);
  39.                         break;
  40.                     case "Reverse":
  41.                         int indexToRemove = commands.LastIndexOf(filterType + ";" + filterParameter);
  42.                         commands.RemoveAt(indexToRemove);
  43.                         break;
  44.                 }
  45.             }
  46.  
  47.             // apply changes to the sequence
  48.             foreach (var command in commands)
  49.             {
  50.                 string[] commandArgs = command.Split(';');
  51.                 string filterType = commandArgs[0];
  52.                 int filterParameter = int.Parse(commandArgs[1]);
  53.  
  54.                 Func<List<int>, int, List<int>> markLExcludeFunc = (list, filterValue) =>
  55.                 {
  56.                     var marked = new List<int>();
  57.                     int leftNum = 0;
  58.                     for (int i = 0; i < list.Count; i++)
  59.                     {
  60.                         if (leftNum + list[i] == filterValue)
  61.                         {
  62.                             marked.Add(list[i]);
  63.                         }
  64.  
  65.                         leftNum = list[i];
  66.                     }
  67.  
  68.                     return marked;
  69.                 };
  70.                 Func<List<int>, int, List<int>> markRExcludeFunc = (list, filterValue) =>
  71.                 {
  72.                     var marked = new List<int>();
  73.                     for (int i = 0; i < list.Count; i++)
  74.                     {
  75.                         if (i + 1 >= list.Count)
  76.                         {
  77.                             if (list[i] == filterValue)
  78.                             {
  79.                                 marked.Add(list[i]);
  80.                             }
  81.                         }
  82.                         else if (list[i] + list[i + 1] == filterValue)
  83.                         {
  84.                             marked.Add(list[i]);
  85.                         }
  86.                     }
  87.  
  88.                     return marked;
  89.                 };
  90.                 Func<List<int>, int, List<int>> markLRExcludeFunc = (list, filterValue) =>
  91.                 {
  92.                     var marked = new List<int>();
  93.                     int leftElement = 0;
  94.                     for (int i = 0; i < list.Count; i++)
  95.                     {
  96.                         int currentElement = list[i];
  97.                         if (i + 1 >= list.Count)
  98.                         {
  99.                             if (leftElement + currentElement == filterValue)
  100.                             {
  101.                                 marked.Add(currentElement);
  102.                             }
  103.                         }
  104.                         else
  105.                         {
  106.                             int rightElement = list[i + 1];
  107.                             if (leftElement + currentElement + rightElement == filterValue)
  108.                             {
  109.                                 marked.Add(list[i]);
  110.                             }
  111.                         }
  112.  
  113.                         leftElement = list[i];
  114.                     }
  115.  
  116.                     return marked;
  117.                 };
  118.                 Action<List<int>, List<int>> excludeAction = (listToModify, listToExclude) =>
  119.                 {
  120.                     foreach (var num in listToExclude)
  121.                     {
  122.                         if (listToModify.Contains(num))
  123.                         {
  124.                             listToModify.Remove(num);
  125.                         }
  126.                     }
  127.                 };
  128.  
  129.                 switch (filterType)
  130.                 {
  131.                     case "Sum Left":
  132.                         var toExclude = markLExcludeFunc(sequence, filterParameter);
  133.                         excludeAction(sequence, toExclude);
  134.                         break;
  135.                     case "Sum Right":
  136.                         toExclude = markRExcludeFunc(sequence, filterParameter);
  137.                         excludeAction(sequence, toExclude);
  138.                         break;
  139.                     case "Sum Left Right":
  140.                         toExclude = markLRExcludeFunc(sequence, filterParameter);
  141.                         excludeAction(sequence, toExclude);
  142.                         break;
  143.                 }
  144.             }
  145.  
  146.             // print
  147.             printAction(sequence);
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement