-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
64 lines (50 loc) · 1.55 KB
/
Solution.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
using System.Linq;
using System.Security.Cryptography;
namespace AdventOfCode2016.Day05
{
internal class Solution
{
private const int PasswordLength = 8;
private readonly string _input;
public Solution(string input)
{
_input = input;
}
public string PartOne()
{
using var md5 = MD5.Create();
var i = 0;
var password = "";
while (password.Length < PasswordLength)
{
var hash = $"{_input}{i}".Hash(md5);
if (hash.StartsWith("00000"))
{
password += hash[5];
}
i++;
}
return password.ToLower();
}
public string PartTwo()
{
using var md5 = MD5.Create();
var i = 0;
var password = Enumerable.Repeat('_', PasswordLength).ToArray();
while (password.Contains('_'))
{
var hash = $"{_input}{i}".Hash(md5);
if (hash.StartsWith("00000"))
{
var position = (int) char.GetNumericValue(hash[5]);
if (position >= 0 && position < PasswordLength && password[position] == '_')
{
password[position] = hash[6];
}
}
i++;
}
return string.Concat(password).ToLower();
}
}
}