-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016_05.cs
174 lines (151 loc) · 4.65 KB
/
2016_05.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System.Security.Cryptography;
using System.Text;
namespace AOC.CSharp;
public static class AOC2016_05
{
const int ChunkSize = 800000;
const int ParallelWorkers = 8;
public static string Solve1(string[] lines)
{
string prefix = lines[0];
string password = "";
int i = 0;
while (password.Length < 8)
{
ValidHashResult[] results = TryGenerateResultsChunk(
prefix,
i,
i + ChunkSize,
password.Length,
ParallelWorkers
);
for (int j = 0; password.Length < 8 && j < results.Length; j++)
{
ValidHashResult result = results[j];
password = password + result.Value;
}
i += ChunkSize;
}
return password;
}
public static string Solve2(string[] lines)
{
const int ChunkSize = 800000;
string prefix = lines[0];
char[] password = new char[8];
int passwordChars = 0;
int i = 0;
while (passwordChars < 8)
{
ValidHashResult[] results = TryGenerateResultsChunk(
prefix,
i,
i + ChunkSize,
null,
ParallelWorkers
);
for (int j = 0; passwordChars < 8 && j < results.Length; j++)
{
ValidHashResult result = results[j];
if (password[result.Position] == '\0')
{
password[result.Position] = result.Value;
passwordChars++;
}
}
i += ChunkSize;
}
return new string(password);
}
private static ValidHashResult[] TryGenerateResultsChunk(
string prefix,
int startIdxInc,
int endIdxExc,
int? pos,
int parallel
)
{
int totalToCalc = endIdxExc - startIdxInc;
int workSize = totalToCalc / parallel;
if (workSize * parallel != totalToCalc)
{
throw new InvalidOperationException("Make sure range is divisible by parallel");
}
ValidHashResult[] arr = new ValidHashResult[totalToCalc];
List<Tuple<int, int>> ranges = new();
for (int i = 0; i < parallel; i++)
{
int start = startIdxInc + (i * workSize);
int end = start + workSize;
ranges.Add(Tuple.Create(start, end));
}
Parallel.For(
0,
parallel,
i =>
{
int arrOffset = i * workSize;
ComputeHashResultsSingleWorker(
arr,
prefix,
arrOffset,
ranges[i].Item1,
ranges[i].Item2,
pos
);
}
);
return arr.Where(a => a != null).ToArray();
}
private static void ComputeHashResultsSingleWorker(
ValidHashResult[] arr,
string prefix,
int arrOffset,
int startIdxInc,
int endIdxExc,
int? pos
)
{
StringBuilder hashSb = new();
MD5 md5 = MD5.Create();
for (int i = startIdxInc; i < endIdxExc; i++)
{
string toHash = prefix + i;
arr[arrOffset + i - startIdxInc] = TryGetHashResult(md5, hashSb, toHash, pos);
}
}
private static ValidHashResult TryGetHashResult(
MD5 md5,
StringBuilder hashSb,
string toHash,
int? pos
)
{
byte[] bytes = Encoding.ASCII.GetBytes(toHash);
byte[] hashed = md5.ComputeHash(bytes);
if (hashed[0] == 0 && hashed[1] == 0 && (hashed[2] & 0xF0) == 0)
{
// Compute the hash as a hex string
hashSb.Clear();
for (int i = 0; i < hashed.Length; i++)
{
hashSb.Append(hashed[i].ToString("x2"));
}
string asStr = hashSb.ToString();
// If the position was passed in, use it. Otherwise derive it from the hash
int finalPos = pos.HasValue
? pos.Value
: (asStr[5] >= '0' && asStr[5] <= '7')
? asStr[5] - '0'
: -1;
// Vary where we find the password value based on whether or not we were passed a position
if (finalPos > -1)
{
char value = pos.HasValue ? asStr[5] : asStr[6];
return new ValidHashResult(finalPos, value);
}
}
return null;
}
private record ValidHashResult(int Position, char Value);
}