Advertisement
Guest User

SuperClass for 22-solve by MrPaul

a guest
Feb 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. public class Calculation
  2.     {
  3.         public delegate int Command(int x);
  4.  
  5.         private List<Command> commands = new List<Command>();
  6.         public void RegisterCommand(Command command) => commands.Add(command);
  7.  
  8.         public long GetCount(int begin, int[] checkPointsOrder, int[] deprecated = null, Func<int, bool> stop = null)
  9.         {
  10.             if (checkPointsOrder == null || checkPointsOrder.Length == 0) return 0;
  11.             if (deprecated == null) deprecated = new int[]{ };
  12.             if (stop == null) stop = x => false;
  13.  
  14.             SortedSet<int> depr = new SortedSet<int>(deprecated);
  15.             int Run(int x, int currEnd)
  16.             {
  17.                 if (depr.Contains(x) || stop(x)) return 0;
  18.                 if (x == currEnd) return 1;
  19.                 try
  20.                 {
  21.                     return commands.Select(f => Run(checked(f(x)), currEnd)).Sum();
  22.                 }
  23.                 catch(Exception)
  24.                 {
  25.                     return 0;
  26.                 }
  27.             }
  28.  
  29.             long result = 1;
  30.            
  31.             foreach(int end in checkPointsOrder)
  32.             {
  33.                 result *= Run(begin, end);
  34.                 begin = end;
  35.             }
  36.  
  37.             return result;
  38.         }
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement