-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19-2.linq
More file actions
64 lines (55 loc) · 1.63 KB
/
19-2.linq
File metadata and controls
64 lines (55 loc) · 1.63 KB
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
<Query Kind="Program" />
static int _leastSteps = 200;
static Dictionary<string, string> _replacements = new Dictionary<string, string>();
static string _target = "e";
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
var inputLines = File.ReadAllLines("19.txt");
foreach (var inputLine in inputLines.Where(il => il.Contains("=>")))
{
string[] split = inputLine.Split(' ');
_replacements.Add(split[2], split[0]);
}
string molecule = inputLines.Last();
Process(molecule, 0);
_leastSteps.Dump();
}
static void Process(string inputString, int counter)
{
if (counter >= _leastSteps) return;
if (inputString == _target)
{
_leastSteps = counter;
_leastSteps.Dump();
return;
}
var replacements = Shuffle(_replacements.Select(kvp => kvp).ToList());
foreach (var sub in replacements)
{
if (inputString.Contains(sub.Key))
{
for (int i = 0; i < inputString.Length + 1 - sub.Key.Length; i++)
{
if (new string(inputString.Skip(i).Take(sub.Key.Length).ToArray()) == sub.Key)
{
Process(inputString.Substring(0, i) + sub.Value + inputString.Substring(i + sub.Key.Length), counter + 1);
}
}
}
}
}
public static IList<T> Shuffle<T>(IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
return list;
}