Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from node in new HtmlWeb().Load("http://regex.alf.nu/").DocumentNode.SelectNodes("//script")
- let script = node.InnerText
- let json = Regex.Match(script, @"levels = (\[.+?\]);", RegexOptions.Singleline).Groups[1].Value
- let fixedJson = Regex.Replace(json, @"x\((\d+)\)", m => "'" + new string('x', int.Parse(m.Groups[1].Value)) + "'")
- let levels = JArray.Parse(fixedJson)
- let alphabet = @".[](){}*+-?!=^$\|<>".AsEnumerable()
- .Concat(Enumerable.Range('a', 'z'-'a'+1).Concat(Enumerable.Range('0', 10)).Select(c=>(char)c))
- .ToList()
- let combinator = new Func<Func<int, IEnumerable<string>>>(()=>
- {
- Func<int, IEnumerable<string>> combinator = null;
- combinator = length =>
- from start in length > 1 ? combinator(length - 1) : new[]{""}
- let combinations =
- from c in alphabet
- select start + c
- from combination in new[]{start}.Concat(combinations)
- select combination;
- return combinator;
- })()
- let sw = Stopwatch.StartNew()
- from level in levels
- let name = (string) level["name"]
- let good = from word in level["good"] select (string) word
- let bad = from word in level["bad"] select (string) word
- let solutions = from pattern in combinator(5)
- let regex = new Func<Regex>(() => { try { return new Regex(pattern); } catch { return null; } })()
- where regex != null
- where good.All(word => regex.IsMatch(word))
- where bad.All(word => !regex.IsMatch(word))
- select pattern
- select new { name, solution = solutions.FirstOrDefault(), elapsed = sw.Elapsed }.Dump()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement