Advertisement
DarkDaskin

Untitled

Dec 24th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. from node in new HtmlWeb().Load("http://regex.alf.nu/").DocumentNode.SelectNodes("//script")
  2. let script = node.InnerText
  3. let json = Regex.Match(script, @"levels = (\[.+?\]);", RegexOptions.Singleline).Groups[1].Value
  4. let fixedJson = Regex.Replace(json, @"x\((\d+)\)", m => "'" + new string('x', int.Parse(m.Groups[1].Value)) + "'")
  5. let levels = JArray.Parse(fixedJson)
  6. let alphabet = @".[](){}*+-?!=^$\|<>".AsEnumerable()
  7.     .Concat(Enumerable.Range('a', 'z'-'a'+1).Concat(Enumerable.Range('0', 10)).Select(c=>(char)c))
  8.     .ToList()
  9. let combinator = new Func<Func<int, IEnumerable<string>>>(()=>
  10. {
  11.     Func<int, IEnumerable<string>> combinator = null;
  12.     combinator = length =>
  13.         from start in length > 1 ? combinator(length - 1) : new[]{""}
  14.         let combinations =
  15.             from c in alphabet
  16.             select start + c
  17.         from combination in new[]{start}.Concat(combinations)
  18.         select combination;
  19.     return combinator;
  20. })()
  21. let sw = Stopwatch.StartNew()
  22. from level in levels
  23. let name = (string) level["name"]
  24. let good = from word in level["good"] select (string) word
  25. let bad = from word in level["bad"] select (string) word
  26. let solutions = from pattern in combinator(5)
  27.     let regex = new Func<Regex>(() => { try { return new Regex(pattern); } catch { return null; } })()
  28.     where regex != null
  29.     where good.All(word => regex.IsMatch(word))
  30.     where bad.All(word => !regex.IsMatch(word))
  31.     select pattern
  32. select new { name, solution = solutions.FirstOrDefault(), elapsed = sw.Elapsed }.Dump()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement