Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code designed to work in LinqPad (this is where the Dump() extension method comes from)
- void Main()
- {
- List<double> inputs = new List<double>(){1,5,6,7};
- Queue<State> workingStates = new Queue<State>();
- workingStates.Enqueue(State.GetInitialState(inputs));
- var finalStates = new List<State>();
- var intermediateStates = new List<State>();
- while (workingStates.Any())
- {
- var currentState = workingStates.Dequeue();
- intermediateStates.Add(currentState);
- for (int i = 0; i<currentState.Values.Count(); i++)
- {
- for (int j = 0; j<currentState.Values.Count(); j++)
- {
- if (i==j)
- continue;
- var newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Add);
- workingStates.Enqueue(newState);
- newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Subtract);
- workingStates.Enqueue(newState);
- newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Multiply);
- workingStates.Enqueue(newState);
- newState = currentState.DoOperation(currentState.Values[i], currentState.Values[j], Operator.Divide);
- workingStates.Enqueue(newState);
- }
- }
- }
- intermediateStates.Where(x=>x.Values.Count()==4).Count().Dump();
- intermediateStates.Where(x=>x.Values.Count()==3).Count().Dump();
- intermediateStates.Where(x=>x.Values.Count()==2).Count().Dump();
- intermediateStates.Where(x=>x.Values.Count()==1).Count().Dump();
- var results = intermediateStates.Where(x=>x.Values.Count()==1).Select(x=>x.Values.Single()).Distinct();
- var intResults = results.Where(x=>Math.Abs(x-Math.Round(x))<0.001).Where(x=>x>0).OrderBy (x => x).Dump();
- //intermediateStates.Where(x=>x.Values.Count()==1).Where(x=>Math.Abs(x.Values.Single()-15)<0.01).Dump();
- foreach (int target in Enumerable.Range(1,200))
- {
- intermediateStates.Where(x=>x.Values.Count()==1).Where(x=>Math.Abs(x.Values.Single()-target)<0.01).Dump();
- }
- }
- // Define other methods and classes here
- public enum Operator
- {
- Add,
- Subtract,
- Multiply,
- Divide
- }
- public class Operation
- {
- double Left {get; set;}
- double Right {get; set;}
- Operator Operator {get; set;}
- public Operation(double left, double right, Operator op)
- {
- Left = left;
- Right = right;
- Operator = op;
- }
- public override string ToString()
- {
- return String.Format("{0} : {1} : {2}", Left, Right, Operator);
- }
- }
- public class State
- {
- public List<double> Values {get; set;}
- public IEnumerable<Operation> Operations {get; set;}
- private State(List<double> values, IEnumerable<Operation> operations)
- {
- Values = values;
- Operations = operations;
- }
- public static State GetInitialState(List<double> values)
- {
- return new State(values, new Queue<Operation>());
- }
- public State DoOperation(double left, double right, Operator op)
- {
- double opResult;
- switch(op)
- {
- case Operator.Add:
- opResult = left+right;
- break;
- case Operator.Subtract:
- opResult = left-right;
- break;
- case Operator.Multiply:
- opResult = left*right;
- break;
- case Operator.Divide:
- opResult = left/right;
- break;
- default:
- throw new InvalidOperationException();
- }
- var newValues = new List<double>();
- var leftRemoved = false;
- var rightRemoved = false;
- foreach(var value in Values)
- {
- if (value==left && !leftRemoved)
- {
- leftRemoved = true;
- continue;
- }
- if (value==right && !rightRemoved)
- {
- rightRemoved = true;
- continue;
- }
- newValues.Add(value);
- }
- newValues.Add(opResult);
- if (!leftRemoved || !rightRemoved)
- {
- newValues.Dump();
- left.Dump();
- right.Dump();
- throw new Exception("Didn't remove both things");
- }
- var newOperations = new Queue<Operation>(Operations);
- newOperations.Enqueue(new Operation(left, right, op));
- return new State(newValues, newOperations);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement