-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-1.linq
52 lines (44 loc) · 1.65 KB
/
17-1.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
<Query Kind="Program">
<Namespace>System.Security.Cryptography</Namespace>
</Query>
void Main()
{
string startingPasscode = "vwbaicqe";
int startingX = 0;
int startingY = 0;
int bestSolution = int.MaxValue;
string bestPath = string.Empty;
int maxAttempt = 40;
ProcessPosition(startingX, startingY, startingPasscode);
$"P1: {bestPath.Substring(startingPasscode.Length)}".Dump();
void ProcessPosition(int x, int y, string passcode)
{
if (passcode.Length > maxAttempt) return;
if (x == 3 && y == 3 && passcode.Length < bestSolution)
{
bestSolution = passcode.Length;
bestPath = passcode;
return;
}
string hash = MD5Hash(passcode);
bool canUp = "bcdef".Contains(hash[0]);
bool canDown = "bcdef".Contains(hash[1]);
bool canLeft = "bcdef".Contains(hash[2]);
bool canRight = "bcdef".Contains(hash[3]);
if (canUp && y > 0) ProcessPosition(x, y-1, passcode + "U");
if (canDown && y < 3) ProcessPosition(x, y+1, passcode + "D");
if (canLeft && x > 0) ProcessPosition(x-1, y, passcode + "L");
if (canRight && x < 3) ProcessPosition(x+1, y, passcode + "R");
}
}
public string MD5Hash(string inputString)
{
using (var md5 = MD5.Create())
{
var inputBytes = Encoding.UTF8.GetBytes(inputString);
var hashBytes = md5.ComputeHash(inputBytes);
var sb = new StringBuilder();
for (var i = 0; i < hashBytes.Length; i++) sb.Append(hashBytes[i].ToString("x2"));
return sb.ToString();
}
}