|
| 1 | +using System.Text.RegularExpressions; |
| 2 | + |
| 3 | +namespace LinkSolver { |
| 4 | + public record struct Node(string Value, int Depth, List<string> Path); |
| 5 | + |
| 6 | + public partial class WikiSolver { |
| 7 | + private const string WIKI = "/wiki/"; |
| 8 | + private const string WIKI_URL_FORMAT = "https://{0}.wikipedia.org/wiki/"; |
| 9 | + |
| 10 | + private readonly Regex _regex = MyRegex(); |
| 11 | + private string? _wikiUrl = string.Empty; |
| 12 | + |
| 13 | + public uint MaximumDepth; |
| 14 | + |
| 15 | + public WikiSolver(uint maxDepth = 3) { |
| 16 | + MaximumDepth = maxDepth; |
| 17 | + } |
| 18 | + |
| 19 | + public async Task Run(string start, string target, string language = "en") { |
| 20 | + if (start == null || start.Length == 0 || target == null || target.Length == 0) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + _wikiUrl = string.Format(WIKI_URL_FORMAT, language); |
| 25 | + Console.WriteLine(_wikiUrl); |
| 26 | + |
| 27 | + var queue = new Queue<Node>(); |
| 28 | + queue.Enqueue(new Node(start, 0, new List<string>() { start })); |
| 29 | + |
| 30 | + var found = await IterateOnQueue(queue, target); |
| 31 | + PrintResult(found); |
| 32 | + } |
| 33 | + |
| 34 | + private async Task<Node> IterateOnQueue(Queue<Node> queue, string target) { |
| 35 | + Node found = default; |
| 36 | + var client = new HttpClient(); |
| 37 | + |
| 38 | + while (found == default && queue.Any()) { |
| 39 | + var current = queue.Dequeue(); |
| 40 | + |
| 41 | + Console.WriteLine($"Working on {current.Value} in depth {current.Depth}"); |
| 42 | + |
| 43 | + if (current.Depth == MaximumDepth) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + var wikiPage = await GetWikiPage(client, current.Value); |
| 48 | + found = RunOnLinks(wikiPage, target, current, queue); |
| 49 | + }; |
| 50 | + |
| 51 | + return found; |
| 52 | + } |
| 53 | + |
| 54 | + private async Task<string> GetWikiPage(HttpClient client, string page) { |
| 55 | + var response = await client.GetAsync($"{_wikiUrl}{page}"); |
| 56 | + return await response.Content.ReadAsStringAsync(); |
| 57 | + } |
| 58 | + |
| 59 | + private Node RunOnLinks(string wikiPage, string target, Node current, Queue<Node> queue) { |
| 60 | + foreach (var match in _regex.Matches(wikiPage).ToHashSet<Match>()) { |
| 61 | + var value = match.Groups[1].ToString(); |
| 62 | + |
| 63 | + if (!value.StartsWith(WIKI) || value.Contains('.') || value.Contains(':')) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + value = value.Replace(WIKI, "", StringComparison.OrdinalIgnoreCase); |
| 68 | + |
| 69 | + if (value == target) { |
| 70 | + current.Path.Add(value); |
| 71 | + return current; |
| 72 | + } |
| 73 | + |
| 74 | + var list = new List<string>(current.Path) { value }; |
| 75 | + queue.Enqueue(new Node(value, current.Depth + 1, list)); |
| 76 | + } |
| 77 | + |
| 78 | + return default; |
| 79 | + } |
| 80 | + |
| 81 | + private static void PrintResult(Node result) { |
| 82 | + var isFound = result != default; |
| 83 | + Console.WriteLine("Result is: " + (isFound ? "Found" : "Can\'t link")); |
| 84 | + if (isFound) { |
| 85 | + Console.WriteLine($"The path is: {string.Join(" -> ", result.Path)}"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [GeneratedRegex("href=\"(.*?)\"")] |
| 90 | + private static partial Regex MyRegex(); |
| 91 | + } |
| 92 | +} |
0 commit comments