Created
September 4, 2012 15:56
-
-
Save prabhuignoto/3622716 to your computer and use it in GitHub Desktop.
Code pulling anagrams from the file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Numerics; | |
namespace Euler98 | |
{ | |
class Program | |
{ | |
static FileStream fileStr = new FileStream("words.txt", FileMode.Open, FileAccess.Read); | |
static StreamReader strReader = new StreamReader(fileStr); | |
static string textContent = ""; | |
static List<BigInteger> bingoList = new List<BigInteger>(); | |
static List<string> completedWords = new List<string>(); | |
static List<int> squares = new List<int>(); | |
static List<int> shufflerList = new List<int>(); | |
static void Main(string[] args) | |
{ | |
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); | |
sw.Start(); | |
textContent = strReader.ReadToEnd().Replace("\"", ""); | |
var anagrams_1=textContent.Split(',').Select(str => new CustomString(str)); | |
//find all anagrams in the list. | |
var anagrams = anagrams_1 | |
.GroupBy(cstr => cstr.sortedString) | |
.Where(g => g.Count() > 1) | |
.Select(g => g.Key); | |
bool isSquarePresent=false; | |
int lastSquareIndex = 0; | |
Console.WriteLine("Check complete for "); | |
foreach (var custStr in anagrams) | |
{ | |
var matches = anagrams_1.Where<CustomString>(cstr => cstr.sortedString == custStr); | |
foreach (var match in matches) | |
{ | |
isSquarePresent = false; | |
var counter = 0; | |
var length = match.sortedString.Length; | |
int sqrSeed=0; | |
var limit = Convert.ToInt32(Math.Pow(10,length-1)); | |
Console.WriteLine(match.originalString+" "); | |
while (sqrSeed < limit * 10) | |
{ | |
string sqrSeedString = sqrSeed.ToString(); | |
var isSquared = sqrSeedString | |
.GroupBy(ch => ch) | |
.Where(g => g.Count() > 1); | |
if (sqrSeed.ToString().Length > match.originalString.Length - 1 && isSquared.Count()<1) | |
{ | |
var others = matches.Where<CustomString> | |
(str => str.originalString != match.originalString && | |
!(completedWords.Contains(str.originalString))); | |
foreach (var ele in others) | |
{ | |
//List<char> idxList = new List<char>(); | |
foreach (char c in sqrSeedString) | |
shufflerList.Add('\0'); | |
var startSearchAt = 0; | |
foreach (char ch in match.originalString) | |
{ | |
bool elePlaced = false; | |
while (!elePlaced) | |
{ | |
var idx = ele.originalString.IndexOf(ch, startSearchAt); | |
if (shufflerList[idx] == '\0') | |
{ | |
shufflerList.RemoveAt(idx); | |
shufflerList.Insert(idx, sqrSeedString[match.originalString.IndexOf(ch)]); | |
elePlaced = true; | |
} | |
else | |
{ | |
startSearchAt = idx + 1; | |
} | |
} | |
startSearchAt = 0; | |
} | |
if (shufflerList.First() != '0') | |
{ | |
var chrrrr = shufflerList.ToArray(); | |
dynamic anagramOthNumber = ""; | |
foreach (char ch in chrrrr) | |
anagramOthNumber = anagramOthNumber + ch; | |
anagramOthNumber = Convert.ToInt32(anagramOthNumber); | |
var sqrRoot = Math.Sqrt(anagramOthNumber); | |
if(sqrRoot - Math.Truncate(sqrRoot) == 0.0) | |
bingoList.Add(anagramOthNumber); | |
} | |
shufflerList.Clear(); | |
} | |
//} | |
} | |
if (!isSquarePresent) | |
{ | |
counter++; | |
sqrSeed = counter * counter; | |
if (squares.Count > 0) | |
{ | |
if (squares.Last() > sqrSeed) | |
{ | |
isSquarePresent = true; | |
lastSquareIndex = squares.IndexOf(counter); | |
sqrSeed = squares[lastSquareIndex]; | |
} | |
else | |
squares.Add(sqrSeed); | |
} | |
} | |
else | |
{ | |
if (squares.Count > lastSquareIndex + 1) | |
sqrSeed = squares[++lastSquareIndex]; | |
else | |
{ | |
isSquarePresent = false; | |
counter = Convert.ToInt32(Math.Sqrt(sqrSeed))+1; | |
sqrSeed = counter * counter; | |
squares.Add(sqrSeed); | |
} | |
} | |
} | |
completedWords.Add(match.originalString); | |
} | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.ElapsedMilliseconds); | |
Console.ReadLine(); | |
} | |
struct CustomString | |
{ | |
public char[] charArrayOrg; | |
public char[] sortedCharArr; | |
public string sortedString; | |
public string originalString; | |
public CustomString(String source) | |
{ | |
charArrayOrg = source.ToCharArray(); | |
sortedCharArr = source.ToCharArray(); | |
Array.Sort(sortedCharArr); | |
sortedString = new String(sortedCharArr); | |
originalString = new String(charArrayOrg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment