Advertisement
ipilot

Some C# Insanity

Dec 19th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace CodeRetreate
  9. {
  10.     public static class CodeRetreat
  11.     {
  12.         public static int Sum(int a, int b)
  13.         {
  14.             return SumRec(a, b, 0, false);
  15.         }
  16.  
  17.         public static int Substract(int a, int b)
  18.         {
  19.             return Sum(a, Sum(~b, 1));
  20.         }
  21.  
  22.         private static int SumRec(int a, int b, int index, bool overflow)
  23.         {
  24.             return IF(index == sizeof(int)*8, () => a, () =>
  25.             {
  26.                 var bita = ((a >> index) & 1) == 1;
  27.                 var bitb = ((b >> index) & 1) == 1;
  28.                 a = IF(bita ^ bitb ^ overflow,
  29.                     () => a | (1 << index),
  30.                     () => a & ~(1 << index));
  31.                 overflow = bita & overflow | bitb & overflow | bita & bitb;
  32.                 return SumRec(a, b, index + 1, overflow);
  33.             });
  34.         }
  35.  
  36.         public static int SumByPairs(int[] array, int l, int r)
  37.         {
  38.             return IF(Substract(r, l) <= 1, () => array[l], () =>
  39.             {
  40.                 var centerIndex = (int) Math.Pow(2, Math.Truncate(Math.Log(Substract(r, l), 2)));
  41.                 IF(Sum(l, centerIndex) == r, () => centerIndex /= 2, () => { });
  42.                 centerIndex = Sum(centerIndex, l);
  43.                 var leftValue = SumByPairs(array, l, centerIndex);
  44.                 var rightValue = SumByPairs(array, centerIndex, r);
  45.                 return IF(leftValue > rightValue,
  46.                     () => Substract(leftValue, rightValue),
  47.                     () => Sum(leftValue, rightValue));
  48.             });
  49.         }
  50.  
  51.         public static void IF(bool condition, Action actionIfTrue, Action actionIfFalse)
  52.         {
  53.             var actions = new[] {actionIfFalse, actionIfTrue};
  54.             actions[Convert.ToInt32(condition)]();
  55.         }
  56.  
  57.         public static T IF<T>(bool condition, Func<T> actionIfTrue, Func<T> actionIfFalse)
  58.         {
  59.             var actions = new[] {actionIfFalse, actionIfTrue};
  60.             return actions[Convert.ToInt32(condition)]();
  61.         }
  62.     }
  63.  
  64.     class Program
  65.     {
  66.         static void Main()
  67.         {
  68.             var data = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
  69.             var sum = 0;
  70.             for (var i = 0; i < data.Length; i = CodeRetreat.Sum(i, 1))
  71.                 sum = CodeRetreat.Sum(sum, data[i]);
  72.             Console.WriteLine(sum);
  73.  
  74.             var sumAndSubstract = data[0];
  75.             for (var i = 1; i < data.Length; i = CodeRetreat.Sum(i, 1))
  76.                 sumAndSubstract = CodeRetreat.IF(data[i] > data[i - 1],
  77.                     () => CodeRetreat.Sum(sumAndSubstract, data[i]),
  78.                     () => CodeRetreat.Substract(sumAndSubstract, data[i]));
  79.             Console.WriteLine(sumAndSubstract);
  80.  
  81.             var sumByPairs = CodeRetreat.SumByPairs(data, 0, data.Length);
  82.             Console.WriteLine(sumByPairs);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement