-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.cs
62 lines (52 loc) · 1.95 KB
/
Day5.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2016
{
class Day5
{
public static string Part1()
{
string doorId = "reyedfim";
string password = "";
for (int i = 0; password.Length < 8; i++)
{
var md5 = System.Security.Cryptography.MD5.Create();
var input = doorId + i.ToString();
var hash = BitConverter.ToString(
md5.ComputeHash(Encoding.ASCII.GetBytes(input))).Replace("-", "");
if (hash.StartsWith("00000"))
{
password += hash[5];
Console.Write(hash[5]);
}
}
return password.ToLower();
}
public static string Part2()
{
string doorId = "reyedfim";
char[] password = "________".ToCharArray();
for (int i = 0; Array.IndexOf(password, '_') >= 0; i++)
{
var md5 = System.Security.Cryptography.MD5.Create();
var input = doorId + i.ToString();
var hash = BitConverter.ToString(
md5.ComputeHash(Encoding.ASCII.GetBytes(input))).Replace("-", "");
if (hash.StartsWith("00000"))
{
int index;
if (int.TryParse(hash[5].ToString(), out index) && index < 8 && password[index] == '_')
{
password[index] = hash[6];
Console.Write(new string(password).ToLower());
Console.CursorLeft = 0;
}
}
}
return new string(password).ToLower();
}
}
}