/// <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); }
//////////////////////////////////////////////////////////////////// // Write object to PDF file //////////////////////////////////////////////////////////////////// internal override void WriteObjectToPdfFile ( BinaryWriter PdfFile ) { // look for first and last character Int32 FirstChar; Int32 LastChar; for (FirstChar = 0; FirstChar < 256 && !ActiveChar[FirstChar]; FirstChar++) { ; } if (FirstChar == 256) { return; } for (LastChar = 255; !ActiveChar[LastChar]; LastChar--) { ; } // pdf font name StringBuilder PdfFontName = new StringBuilder("/"); // for embedded font add 6 alpha characters prefix if (EmbeddedFont) { PdfFontName.Append("PFWAAA+"); Int32 Ptr1 = 6; for (Int32 Ptr2 = ResourceCode.Length - 1; Ptr2 >= 0 && Char.IsDigit(ResourceCode[Ptr2]); Ptr2--) { PdfFontName[Ptr1--] = (Char)((Int32)ResourceCode[Ptr2] + ('A' - '0')); } } // PDF readers are not happy with space in font name PdfFontName.Append(FontFamily.Name.Replace(" ", "#20")); // font name if ((DesignFont.Style & FontStyle.Bold) != 0) { if ((DesignFont.Style & FontStyle.Italic) != 0) { PdfFontName.Append(",BoldItalic"); } else { PdfFontName.Append(",Bold"); } } else if ((DesignFont.Style & FontStyle.Italic) != 0) { PdfFontName.Append(",Italic"); } // add items to dictionary AddToDictionary("/Subtype", "/TrueType"); AddToDictionary("/BaseFont", PdfFontName.ToString()); // add first and last characters AddToDictionary("/FirstChar", FirstChar.ToString()); AddToDictionary("/LastChar", LastChar.ToString()); // create font descriptor FontDescriptor = new PdfObject(Document, false, "/FontDescriptor"); // add link to font object AddToDictionary("/FontDescriptor", FontDescriptor); // font descriptor dictionary FontDescriptor.AddToDictionary("/FontName", PdfFontName.ToString()); // must be the same as BaseFont above FontDescriptor.AddToDictionary("/Flags", ((Int32)FontFlags).ToString()); FontDescriptor.AddToDictionary("/ItalicAngle", String.Format(NFI.DecSep, "{0}", (Single)PdfItalicAngle)); FontDescriptor.AddToDictionary("/FontWeight", PdfFontWeight.ToString()); FontDescriptor.AddToDictionary("/Leading", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(PdfLeading))); FontDescriptor.AddToDictionary("/Ascent", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(PdfAscent))); FontDescriptor.AddToDictionary("/Descent", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(-PdfDescent))); // alphabetic (non symbolic) fonts if ((FontFlags & PdfFontFlags.Symbolic) == 0) { AddToDictionary("/Encoding", "/WinAnsiEncoding"); BoundingBox Box = FontInfo.GetGlyphMetricsApi('x'); FontDescriptor.AddToDictionary("/XHeight", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(Box.Rect.Top))); FontDescriptor.AddToDictionary("/AvgWidth", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(Box.Width))); Box = FontInfo.GetGlyphMetricsApi('M'); PdfCapHeight = Box.Rect.Top; FontDescriptor.AddToDictionary("/CapHeight", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(Box.Rect.Top))); FontDescriptor.AddToDictionary("/StemV", String.Format(NFI.DecSep, "{0}", (Single)StemV())); } // create width object array FontWidthArray = new PdfObject(Document, false); // add link to font object AddToDictionary("/Widths", FontWidthArray); // build bounding box and width array Double Left = Double.MaxValue; Double Bottom = Double.MaxValue; Double Right = Double.MinValue; Double Top = Double.MinValue; Double MaxWidth = Double.MinValue; FontWidthArray.ContentsString = new StringBuilder("["); Int32 EolLength = 100; for (Int32 Index = FirstChar; Index <= LastChar; Index++) { Double CharWidth; // not used if (!ActiveChar[Index]) { CharWidth = 0; } // used else { // bounding box BoundingBox GM = GlyphArray[Index]; if (GM.Rect.Left < Left) { Left = GM.Rect.Left; } if (GM.Rect.Bottom < Bottom) { Bottom = GM.Rect.Bottom; } if (GM.Rect.Right > Right) { Right = GM.Rect.Right; } if (GM.Rect.Top > Top) { Top = GM.Rect.Top; } // character advance width CharWidth = GM.Width; // max width if (CharWidth > MaxWidth) { MaxWidth = CharWidth; } } // add width to width array if (FontWidthArray.ContentsString.Length > EolLength) { FontWidthArray.ContentsString.Append('\n'); EolLength = FontWidthArray.ContentsString.Length + 100; } // add width to width array FontWidthArray.ContentsString.AppendFormat(NFI.DecSep, "{0} ", FontUnitsToPdfDic(CharWidth)); } // add to font descriptor array FontDescriptor.AddToDictionary("/MaxWidth", String.Format(NFI.DecSep, "{0}", (Single)FontUnitsToPdfDic(MaxWidth))); FontDescriptor.AddToDictionary("/FontBBox", String.Format(NFI.DecSep, "[{0} {1} {2} {3}]", FontUnitsToPdfDic(Left), FontUnitsToPdfDic(Bottom), FontUnitsToPdfDic(Right), FontUnitsToPdfDic(Top))); // terminate width array FontWidthArray.ContentsString.Length--; FontWidthArray.ContentsString.Append("]"); // create font file if (EmbeddedFont) { // create font file stream PdfFontFile EmbeddedFontObj = new PdfFontFile(this); // add link to font object FontDescriptor.AddToDictionary("/FontFile2", EmbeddedFontObj); } // call base write PdfObject to file method base.WriteObjectToPdfFile(PdfFile); // exit return; }