public static int Part2(string input) { var inp = input.Split("\n\n"); var rules = inp[0].Split("\n"); var realIn = inp[1].Split("\n"); Dictionary <int, string> splitRules = new(); foreach (var(i, r) in rules.Select(s => { var ss = s.Split(": "); return(int.Parse(ss[0]), ss[1]); })) { splitRules.Add(i, r); } splitRules[8] = "42 | 42 8"; splitRules[11] = "42 31 | 42 11 31"; Dictionary <int, Parser> parsers = new(); Parser GetParser(int indx) { if (parsers.ContainsKey(indx)) { return(parsers[indx]); } parsers[indx] = inp => GetParser(indx)(inp); parsers[indx] = Alt((from seq in splitRules[indx].Split(" | ") select Seq((from i in seq.Split(" ") select int.TryParse(i, out var n) ? GetParser(n) : Literal(i.Trim('"'))).ToList())) .ToList()); return(parsers[indx]); } Parser Alt(List <Parser> parsers) => parsers.Count == 1 ? parsers.Single() : input => from parser in parsers.ToArray() from rest in parser(input) select rest; Parser Seq(List <Parser> parsers) => parsers.Count == 1 ? parsers.Single() : inpt => from tail in parsers.First()(inpt) from rest in Seq(parsers.Skip(1).ToList())(tail) select rest; Parser Literal(string st) => inpt => inpt.StartsWith(st) ? new[] { inpt.Substring(st.Length) } : new string[0]; var parser = GetParser(0); return(realIn.Count(data => parser(data).Any(st => st == ""))); }
public static bool TryParse(string s, out Activity result) { result = null; if (string.IsNullOrWhiteSpace(s)) { return(false); } var items = s.Split(new char[] { ' ' }, 3); if (items.Length != 3) { return(false); } var dT = new Timestamp(); if (!long.TryParse(items[0], out dT.Milliseconds)) { return(false); } int type; if (!int.TryParse(items[1], out type)) { return(false); } Category cat = Categories.get(type); if (cat == null) { return(false); } IData data; if (!GetParser(cat)(items[2], out data)) { return(false); } result = new Activity() { Timestamp = dT, Type = cat, Info = data }; return(true); }
private string ReplaceTokens(string s) { if (s.IndexOf("(!") == -1) { return(s); } string tmpS = string.Empty; // process SKIN specific tokens here only: s = s.Replace("(!SECTION_TITLE!)", SectionTitle); if (SectionTitle.Length != 0) { s = s.Replace("(!SUPERSECTIONTITLE!)", "<div align=\"left\"><span class=\"SectionTitleText\">" + SectionTitle + "</span><br/><small> </small></div>"); } else { s = s.Replace("(!SUPERSECTIONTITLE!)", ""); } s = s.Replace("(!METATITLE!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SETitle), m_SETitle, m_SETitle.ToHtmlEncode())); s = s.Replace("(!METADESCRIPTION!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEDescription), m_SEDescription, m_SEDescription.ToHtmlEncode())); s = s.Replace("(!METAKEYWORDS!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEKeywords), m_SEKeywords, m_SEKeywords.ToHtmlEncode())); s = s.Replace("(!SENOSCRIPT!)", m_SENoScript); s = GetParser.ReplaceTokens(s); return(s); }
private string ReplaceTokens(String s) { if (s.IndexOf("(!") == -1) { return(s); } // process SKIN specific tokens here only: s = s.Replace("(!SECTION_TITLE!)", SectionTitle); if (SectionTitle.Length != 0) { s = s.Replace("(!SUPERSECTIONTITLE!)", "<div align=\"left\"><span class=\"SectionTitleText\">" + SectionTitle + "</span><small> </small></div>"); if (AppLogic.IsAdminSite) { s = s.Replace("class=\"SectionTitleText\"", "class=\"breadCrumb3\""); } } else { s = s.Replace("(!SUPERSECTIONTITLE!)", ""); } s = s.Replace("(!METATITLE!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SETitle), m_SETitle, HttpContext.Current.Server.HtmlEncode(m_SETitle))); s = s.Replace("(!METADESCRIPTION!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEDescription), m_SEDescription, HttpContext.Current.Server.HtmlEncode(m_SEDescription))); s = s.Replace("(!METAKEYWORDS!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEKeywords), m_SEKeywords, HttpContext.Current.Server.HtmlEncode(m_SEKeywords))); s = GetParser.ReplaceTokens(s); return(s); }
private string ReplaceTokens(String s) { if (s.IndexOf("(!") == -1) { return(s); } String tmpS = String.Empty; // process SKIN specific tokens here only: s = s.Replace("(!SECTION_TITLE!)", SectionTitle); if (SectionTitle.Length != 0) { s = s.Replace("(!SUPERSECTIONTITLE!)", "<div align=\"left\"><span class=\"SectionTitleText\">" + SectionTitle + "</span><small> </small></div>"); if (AppLogic.IsAdminSite) { s = s.Replace("class=\"SectionTitleText\"", "class=\"breadCrumb3\""); } } else { s = s.Replace("(!SUPERSECTIONTITLE!)", ""); } s = s.Replace("(!METATITLE!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SETitle), m_SETitle, HttpContext.Current.Server.HtmlEncode(m_SETitle))); s = s.Replace("(!METADESCRIPTION!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEDescription), m_SEDescription, HttpContext.Current.Server.HtmlEncode(m_SEDescription))); s = s.Replace("(!METAKEYWORDS!)", CommonLogic.IIF(CommonLogic.StringIsAlreadyHTMLEncoded(m_SEKeywords), m_SEKeywords, HttpContext.Current.Server.HtmlEncode(m_SEKeywords))); s = s.Replace("(!SENOSCRIPT!)", m_SENoScript); if (m_GraphicsColor.Length == 0) { m_GraphicsColor = AppLogic.AppConfig("GraphicsColorDefault"); if (m_GraphicsColor.Length == 0) { m_GraphicsColor = String.Empty; } } if (m_ContentsBGColor.Length == 0) { m_ContentsBGColor = AppLogic.AppConfig("ContentsBGColorDefault"); if (m_ContentsBGColor.Length == 0) { m_ContentsBGColor = "#FFFFFF"; } } if (m_PageBGColor.Length == 0) { m_PageBGColor = AppLogic.AppConfig("PageBGColorDefault"); if (m_PageBGColor.Length == 0) { m_PageBGColor = "#FFFFFF"; } } s = s.Replace("(!GRAPHICSCOLOR!)", m_GraphicsColor); s = s.Replace("(!CONTENTSBGCOLOR!)", m_ContentsBGColor); s = s.Replace("(!PAGEBGCOLOR!)", m_PageBGColor); s = GetParser.ReplaceTokens(s); return(s); }