/// <summary> /// https://projecteuler.net/problem=59 /// </summary> /// <param name="arguments"></param> /// <returns></returns> public static Result XorDecryption(Problem arguments) { const char FirstChar = 'a'; const char LastChar = 'z'; var sum = 0; var key = string.Empty; var text = string.Empty; var cvsinfo = arguments.Sequence.Split('|'); var fileName = cvsinfo[0]; var delimiter = Convert.ToChar(cvsinfo[1]); var qualifier = Convert.ToChar(cvsinfo[2]); var cyphers = UtilityFile.ReadCsvToArray(fileName, delimiter, qualifier); var ascii = cyphers.Select(X => Convert.ToInt32(X)).ToArray(); var allPossiblePasswords = (from a in FirstChar.To(LastChar) from b in FirstChar.To(LastChar) from c in FirstChar.To(LastChar) select new[] { Convert.ToInt32(a), Convert.ToInt32(b), Convert.ToInt32(c) }).ToArray(); foreach (var password in allPossiblePasswords) { var decrypted = UtilityMath.Encrypt(ascii, password); text = new string(decrypted.Select(x => Convert.ToChar(x)).ToArray()); if (text.Contains(" the ")) //perform simple check: if Text contains single word 'the' it is in english { key = new string(password.Select(x => Convert.ToChar(x)).ToArray()); break; } } sum = text.Aggregate(0, (runningTotal, next) => runningTotal += next); var answer = sum.ToString(); var message = string.Format("The sum of the ASCII values in the original English text is {0}.", answer); if (Answers[arguments.Id] != answer) { message += string.Format(" => INCORRECT ({0})", Answers[arguments.Id]); } var r = new Result(arguments.Id, message) { Answer = answer }; return(r); }