-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.linq
62 lines (57 loc) · 1.29 KB
/
11.linq
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
<Query Kind="Program" />
void Main()
{
FindNext("vzbxkghb");
FindNext("vzbxxyzz");
}
void FindNext(string password)
{
char[] passwordChars = password.ToArray();
Increment(passwordChars, passwordChars.Length - 1);
while (!IsValid(passwordChars))
{
Increment(passwordChars, passwordChars.Length - 1);
}
new string(passwordChars).Dump();
}
bool IsValid(char[] password)
{
if (password.Any(c => c == 'i' | c == 'o' || c == 'l')) return false;
bool sequential = false;
for (int i = 0; i < password.Length - 2; i++)
{
if (password[i] == password[i + 1] - 1 && password[i] == password[i + 2] - 2)
{
sequential = true;
}
}
if (!sequential) return false;
int pairs = 0;
for (int i = 0; i < password.Length - 1; i++)
{
if (password[i] == password[i + 1])
{
pairs++;
i++;
}
}
return pairs >= 2;
}
void Increment(char[] password, int index)
{
char c = password[index];
if (c == 'h' || 'c' == 'n' || c == 'k')
{
c = (char)(c + 2);
password[index] = c;
}
else if (c == 'z')
{
password[index] = 'a';
Increment(password, index - 1);
}
else
{
password[index] = (char)(c + 1);
}
}