MiroslavKisov

Inferno III

Feb 7th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Inferno_III
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<int> gems = Console.ReadLine()
  12.                 .Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.             Dictionary<string, Func<List<int>, List<int>>> filters = new Dictionary<string, Func<List<int>, List<int>>>();
  16.             string command;
  17.             while ((command = Console.ReadLine()) != "Forge")
  18.             {
  19.                 ParseCommand(command, filters);
  20.             }
  21.             List<int> filtered = GetFiltered(gems, filters);
  22.  
  23.             gems = gems.Where(gem => !filtered.Contains(gem)).ToList();
  24.             string result = string.Join(" ", gems);
  25.             Console.WriteLine(result);
  26.         }
  27.  
  28.         private static List<int> GetFiltered(List<int> gems, Dictionary<string, Func<List<int>, List<int>>> filters)
  29.         {
  30.             List<int> filtered = new List<int>();
  31.             foreach (var pair in filters)
  32.             {
  33.                 var filter = pair.Value;
  34.                 filtered.AddRange(pair.Value(gems));
  35.             }
  36.             return filtered;
  37.         }
  38.  
  39.         private static void ParseCommand(string commandInput, Dictionary<string, Func<List<int>, List<int>>> filters)
  40.         {
  41.             string[] tokens = commandInput.Split(';');
  42.             string command = tokens[0];
  43.             string filterType = tokens[1];
  44.             int parameter = int.Parse(tokens[2]);
  45.             string dictKey = $"{filterType} {parameter}";
  46.  
  47.             if (command == "Exclude")
  48.             {
  49.                 filters[dictKey] = CreateFunction(filterType, parameter);
  50.             }
  51.             else if (command == "Reverse")
  52.             {
  53.                 filters.Remove(dictKey);
  54.             }
  55.         }
  56.  
  57.         private static Func<List<int>, List<int>> CreateFunction(string filterType, int parameter)
  58.         {
  59.             switch (filterType)
  60.             {
  61.                 case "Sum Left":
  62.                     return gems => gems.Where(gem =>
  63.                     {
  64.                         int index = gems.IndexOf(gem);
  65.                         int leftGem = index > 0 ? gems[index - 1] : 0;
  66.                         return gem + leftGem == parameter;
  67.                     }).ToList();
  68.                 case "Sum Right":
  69.                     return gems => gems.Where(gem =>
  70.                     {
  71.                         int index = gems.IndexOf(gem);
  72.                         int rightGem = index < gems.Count - 1 ? gems[index + 1] : 0;
  73.                         return gem + rightGem == parameter;
  74.                     }).ToList();
  75.                 case "Sum Left Right":
  76.                     return gems => gems.Where(gem =>
  77.                     {
  78.                         int index = gems.IndexOf(gem);
  79.                         int leftGem = index > 0 ? gems[index - 1] : 0;
  80.                         int rightGem = index < gems.Count - 1 ? gems[index + 1] : 0;
  81.                         return leftGem + gem + rightGem == parameter;
  82.                     }).ToList();
  83.                 default:
  84.                     throw new ArgumentException();
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment