Skip to content

Instantly share code, notes, and snippets.

@DennisdeBest
Created December 18, 2016 19:24
Show Gist options
  • Save DennisdeBest/18ffa7916543cffe7de448cd3c8fae52 to your computer and use it in GitHub Desktop.
Save DennisdeBest/18ffa7916543cffe7de448cd3c8fae52 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
namespace Generator
{
public class Solver
{
private Tuple<List<int>, string> list;
private int expectedRes;
//Path for Data File
private string path;
private string pathData;
//List of lists obtained for each cycle
private List<Tuple<List<int>, string>> listOfTuples;
private Tuple<List<int>, string> listWithOperations;
public Solver(List<int> list, int res)
{
List<Tuple<List<int>, string>> DistinctResultList = new List<Tuple<List<int>, string>> { };
//Set path for saved Data
path = "D:\\VisualStudioData\\solver.txt";
pathData = "D:\\VisualStudioData\\data.txt";
File.WriteAllText(path, string.Empty);
//initialise the tuples and variables
this.list = new Tuple<List<int>, string> (list, "" );
this.expectedRes = res;
int listSize = this.list.Item1.Count;
//Create Regex for result
string regStr = " " + Convert.ToString(res) + "$";
var lastNumber = new Regex(@regStr);
//Remove duplicates from the list of tuples
listOfTuples = RemoveDuplicates(AllPossibleResultsOneCycle(this.list));
//Maximum allowed result
//There are 4 cycles of operations so we loop 3 times (first cycle is already done above)
for (int j = 0; j < listSize - 1; j++)
{
//Write the solutions to the file
foreach(Tuple<List<int>, string> element in listOfTuples) {
//File.AppendAllLines(pathData, new List<string> { listToString(element.Item1), element.Item2 });
if (lastNumber.IsMatch(element.Item2))
{
File.AppendAllLines(path, new List<string> { listToString(element.Item1), element.Item2, Environment.NewLine });
}
}
//Initialise a new resultList
List<Tuple<List<int>, string>> ResultList = new List<Tuple<List<int>, string>> { };
//Get a list From the list of lists returned by the last cycle
foreach (Tuple<List<Int32>, string> element in listOfTuples)
{
foreach (Tuple<List<int>, string> cycleResultList in AllPossibleResultsOneCycle(element))
{
//Add the returned list to the new result list
ResultList.Add(cycleResultList);
}
}
//Remove the duplicates before starting the next cycle
listOfTuples = RemoveDuplicates(ResultList);
}
}
//Return a list of all the possible outcomes from one operation cycle
public List<Tuple<List<int>, string>> AllPossibleResultsOneCycle(Tuple<List<int>,string> tuple)
{
int res = 0;
//Operations.Add(Environment.NewLine + listToString(list));
List<Tuple<List<int>, string>> ResultList = new List<Tuple<List<int>, string>> { };
for (int i = 0; i < tuple.Item1.Count; i++)
{
for (int j = 0; j < tuple.Item1.Count; j++)
{
if (i == j)
{
continue;
}
else
{
int a = tuple.Item1[i];
int b = tuple.Item1[j];
for (int k = 0; k < 4; k++)
{
if (k == 0)
{
res = a + b;
CreateAndAddNewList(i, j, res, "+", tuple, ResultList);
}
else if (k == 1)
{
res = a * b;
CreateAndAddNewList(i, j, res, "x", tuple, ResultList);
}
else if (k == 2)
{
if (b != 0 && a % b == 0)
{
if (a == 0)
{
res = 0;
}
else
{
res = a / b;
}
CreateAndAddNewList(i, j, res, "/", tuple, ResultList);
}
else
{
continue;
}
}
else if (k == 3)
{
if (a > b)
{
res = a - b;
CreateAndAddNewList(i, j, res, "-", tuple, ResultList);
}
else
{
continue;
}
}
}
}
}
}
ResultList = RemoveDuplicates(ResultList);
return ResultList;
}
//Create a new list from the old one without the two values used to calculate the result but with the result
private void CreateAndAddNewList(int i, int j, int res, string symbol, Tuple<List<int>, string> tuple, List<Tuple<List<int>, string>> resultTuple)
{
var listClone = new List<int>(tuple.Item1);
string resultString = "";
if (tuple.Item2 != "")
{
resultString += Environment.NewLine;
}
resultString += CreateOperationString(listClone[i], listClone[j], symbol, res);
listClone.RemoveAt(i);
if (i < j)
{
listClone.RemoveAt(j - 1);
}
else
{
listClone.RemoveAt(j);
}
listClone.Add(res);
Tuple<List<int>, string> outputTuple = new Tuple<List<int>, string>(listClone, tuple.Item2 + resultString);
resultTuple.Add(outputTuple);
}
private string CreateOperationString(int a, int b, string operand, int res)
{
if(operand == "x" || operand == "+")
{
if(a < b)
{
int c = a;
a = b;
b = c;
}
}
return Convert.ToString(a) + " " + operand + " " + Convert.ToString(b) + " = " + Convert.ToString(res);
}
public string listToString(List<int> list)
{
string s = "";
foreach (int element in list)
{
s += Convert.ToString(element) + " - ";
}
return s;
}
private List<Tuple<List<int>, string>> RemoveDuplicates(List<Tuple<List<int>, string>> inputList)
{
//Duplicate the input list and create a new output list
List<Tuple<List<int>, string>> cloneList = new List<Tuple<List<int>, string>>(inputList);
List<Tuple<List<int>, string>> outputList = new List<Tuple<List<int>, string>> { };
List<Tuple<List<int>, string>> outputList2 = new List<Tuple<List<int>, string>> { };
for (int i = 0; i < cloneList.Count - 1; i++)
{
Tuple<List<int>, string> currentTupleI = cloneList[i];
for(int j = 0; j < cloneList.Count - 1; j++)
{
Tuple<List<int>, string> currentTupleJ = cloneList[j];
if (i == j)
{
}
else if(currentTupleI.Item2 == currentTupleJ.Item2)
{
cloneList.RemoveAt(j);
}
}
}
return cloneList;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment