Advertisement
chrisvenus

Untitled

Nov 6th, 2014
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. //Code designed to work in LinqPad (this is where the Dump() extension method comes from)
  2.  
  3. void Main()
  4. {
  5.     List<double> inputs = new List<double>(){1,5,6,7};
  6.     Queue<State> workingStates = new Queue<State>();
  7.     workingStates.Enqueue(State.GetInitialState(inputs));
  8.     var finalStates = new List<State>();
  9.     var intermediateStates = new List<State>();
  10.     while (workingStates.Any())
  11.     {
  12.         var currentState = workingStates.Dequeue();
  13.         intermediateStates.Add(currentState);
  14.         for (int i = 0; i<currentState.Values.Count(); i++)
  15.         {
  16.             for (int j = 0; j<currentState.Values.Count(); j++)
  17.             {
  18.                 if (i==j)
  19.                     continue;
  20.                    
  21.                 var newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Add);
  22.                 workingStates.Enqueue(newState);
  23.  
  24.                 newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Subtract);
  25.                 workingStates.Enqueue(newState);
  26.                 newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Multiply);
  27.                 workingStates.Enqueue(newState);
  28.                 newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Divide);
  29.                 workingStates.Enqueue(newState);
  30.  
  31.             }
  32.         }
  33.     }
  34.     intermediateStates.Where(x=>x.Values.Count()==4).Count().Dump();
  35.     intermediateStates.Where(x=>x.Values.Count()==3).Count().Dump();
  36.     intermediateStates.Where(x=>x.Values.Count()==2).Count().Dump();
  37.     intermediateStates.Where(x=>x.Values.Count()==1).Count().Dump();
  38.  
  39.     var results = intermediateStates.Where(x=>x.Values.Count()==1).Select(x=>x.Values.Single()).Distinct();
  40.    
  41.     var intResults = results.Where(x=>Math.Abs(x-Math.Round(x))<0.001).Where(x=>x>0).OrderBy (x => x).Dump();
  42.     //intermediateStates.Where(x=>x.Values.Count()==1).Where(x=>Math.Abs(x.Values.Single()-15)<0.01).Dump();
  43.    
  44.     foreach (int target in Enumerable.Range(1,200))
  45.     {
  46.         intermediateStates.Where(x=>x.Values.Count()==1).Where(x=>Math.Abs(x.Values.Single()-target)<0.01).Dump();
  47.     }
  48.  
  49. }
  50.  
  51. // Define other methods and classes here
  52.  
  53. public enum Operator
  54. {
  55.     Add,
  56.     Subtract,
  57.     Multiply,
  58.     Divide
  59. }
  60.  
  61. public class Operation
  62. {
  63.     double Left {get; set;}
  64.     double Right {get; set;}
  65.     Operator Operator {get; set;}
  66.    
  67.     public Operation(double left, double right, Operator op)
  68.     {
  69.         Left = left;
  70.         Right = right;
  71.         Operator = op;
  72.     }
  73.    
  74.     public override string ToString()
  75.     {
  76.         return String.Format("{0} : {1} : {2}", Left, Right, Operator);
  77.     }
  78. }
  79.  
  80.  
  81. public class State
  82. {
  83.     public List<double> Values {get; set;}
  84.     public IEnumerable<Operation> Operations {get; set;}
  85.    
  86.     private State(List<double> values, IEnumerable<Operation> operations)
  87.     {
  88.         Values = values;
  89.         Operations = operations;
  90.    
  91.     }
  92.    
  93.     public static State GetInitialState(List<double> values)
  94.     {
  95.         return new State(values, new Queue<Operation>());
  96.     }
  97.    
  98.     public State DoOperation(double left, double right, Operator op)
  99.     {
  100.         double opResult;
  101.         switch(op)
  102.         {
  103.             case Operator.Add:
  104.                 opResult = left+right;
  105.             break;
  106.             case Operator.Subtract:
  107.                 opResult = left-right;
  108.             break;
  109.             case Operator.Multiply:
  110.                 opResult = left*right;
  111.             break;
  112.             case Operator.Divide:
  113.                 opResult = left/right;
  114.             break;
  115.             default:
  116.                 throw new InvalidOperationException();
  117.         }
  118.         var newValues = new List<double>();
  119.         var leftRemoved = false;
  120.         var rightRemoved = false;
  121.         foreach(var value in Values)
  122.         {
  123.             if (value==left && !leftRemoved)
  124.             {
  125.                 leftRemoved = true;
  126.                 continue;
  127.             }
  128.             if (value==right && !rightRemoved)
  129.             {
  130.                 rightRemoved = true;
  131.                 continue;
  132.             }
  133.             newValues.Add(value);
  134.         }
  135.         newValues.Add(opResult);
  136.         if (!leftRemoved || !rightRemoved)
  137.         {
  138.             newValues.Dump();
  139.             left.Dump();
  140.             right.Dump();
  141.             throw new Exception("Didn't remove both things");
  142.         }
  143.        
  144.         var newOperations = new Queue<Operation>(Operations);
  145.         newOperations.Enqueue(new Operation(left, right, op));
  146.         return new State(newValues, newOperations);
  147.     }
  148.    
  149.    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement