-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day14.cs
115 lines (100 loc) · 3.52 KB
/
Day14.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2016
{
class Day14 : Day
{
public dynamic Input
{
get
{
return "yjdafjpo";
}
}
public string Part1(dynamic input)
{
return FindKeys(input, 1);
}
public string Part2(dynamic input)
{
return FindKeys(input, 2017);
}
struct Candidate
{
public char character;
public int index;
public string hash;
}
public string FindKeys(string input, int md5rounds)
{
int i = 0;
int found = 0;
int upperbound = int.MaxValue;
var candidates = new HashSet<Candidate>();
var results = new SortedList<int, int>();
while (i < upperbound)
{
var seed = input + i;
var md5 = System.Security.Cryptography.MD5.Create();
var hash = seed;
for (int j = 0; j < md5rounds; j++)
{
hash = BitConverter.ToString(md5.ComputeHash(Encoding.ASCII.GetBytes(hash))).Replace("-", "").ToLower();
}
char seq = '#';
int len = 0;
bool foundtriplet = false;
foreach (var c in hash)
{
if (c == seq)
{
len++;
if (len == 3 && !foundtriplet)
{
candidates.Add(new Candidate() { character = c, index = i, hash = hash });
foundtriplet = true;
}
if (len == 5)
{
foreach (var candidate in new List<Candidate>(candidates))
{
if (candidate.index < i - 1000)
{
candidates.Remove(candidate);
}
else if (candidate.character == c && candidate.index != i)
{
found++;
Console.Write(found + ": " + candidate.index + "-" + candidate.hash + " / " + i + "-" + hash);
Console.CursorLeft = 0;
candidates.Remove(candidate);
results.Add(candidate.index, candidate.index);
if (results.Count > 64)
{
upperbound = results.Keys[63] + 1000;
}
}
}
}
}
else
{
seq = c;
len = 1;
}
}
i++;
}
Utils.ClearLine();
return results.Keys[63].ToString();
}
public void Test()
{
Utils.Test(Part1, "abc", "22728");
Utils.Test(Part2, "abc", "22551");
}
}
}