-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
81 lines (60 loc) · 2.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace WikipediaLinkSolver
{
class Program
{
static async Task Main(string[] args)
{
string firstDefinition = "Map-Reduce";
string searchedDefinition = "High-performance_computing";
int maxDepth = 3;
HttpClient client = new HttpClient();
Regex regex = new Regex($"href=\"(.*?)\"");
HashSet<string> links = new HashSet<string>();
links.Add(firstDefinition);
(string val, int depth, List<string> path) found = default;
Queue<(string val, int depth, List<string> path)> queue = new Queue<(string val, int depth, List<string> path)>();
queue.Enqueue((firstDefinition, 0, new List<string>() { firstDefinition }));
while (found == default && queue.Any())
{
var curr = queue.Dequeue();
Console.WriteLine($"Working on {curr.val} in depth {curr.depth}");
if (curr.depth == maxDepth)
{
continue;
}
var requestUri = $"https://en.wikipedia.org/wiki/{curr.val}";
var res = await client.GetAsync(requestUri);
var strValue = await res.Content.ReadAsStringAsync();
foreach (Match match in regex.Matches(strValue))
{
var s = match.Groups[1].ToString();
if (!s.StartsWith("/wiki/") || s.Contains('.') || s.Contains(':')) // avoid pics
{
continue;
}
s = s.Replace("/wiki/", "");
if (s == searchedDefinition)
{
curr.path.Add(s);
found = curr;
break;
}
var list = new List<string>(curr.path);
list.Add(s);
queue.Enqueue((s, curr.depth+1, list));
}
} ;
Console.WriteLine($"Result is {found != default}");
if (found != default)
{
Console.WriteLine($"The path is {string.Join('-', found.path)}");
}
}
}
}