-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-19-2.js
101 lines (78 loc) · 2.58 KB
/
day-19-2.js
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
// http://stackoverflow.com/questions/9960908/permutations-in-javascript
function permutator (inputArr) {
var results = [];
function permute (arr, memo) {
var cur, memo = memo || [];
for (var i = 0; i < arr.length; i++) {
cur = arr.splice (i, 1);
if (arr.length === 0) {
results.push (memo.concat (cur));
}
permute (arr.slice (), memo.concat (cur));
arr.splice (i, 0, cur [0]);
}
return results;
}
return permute (inputArr);
}
var input = document.getElementsByTagName ("pre") [0].textContent.split ("\n");
// var input = "e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOH\n".split ("\n"); // 3
// var input = "e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOHOHO\n".split ("\n"); // 6
var replacements = [];
// Cache variables
for (var x = 0; x < input.length && input [x] != ""; x ++) {
var variables = input [x].split (" => ");
var replacement = {
from: variables [0],
to: variables [1]
};
replacements.push (replacement);
}
console.log (replacements);
// Cache the objective
var objective = input [input.length - 2];
console.log (objective);
var permutations = permutator (replacements);
console.log (permutations);
// http://stackoverflow.com/questions/3410464/how-to-find-all-occurrences-of-one-string-in-another-in-javascript
function indexes (source, find) {
var result = [];
for (i = 0; i < source.length; ++i) {
// If you want to search case insensitive use
// if (source.substring (i, i + find.length).toLowerCase () == find) {
if (source.substring (i, i + find.length) == find) {
result.push (i);
}
}
return result;
}
// http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript?rq=1
String.prototype.replaceAt = function (index, from, to) {
return this.substr (0, index) + this.substr (index).replace (from, to);
}
var least = 90000000000;
var permutation;
var steps = 0;
var result;
for (var x = 0; x < permutations.length; x ++) {
steps = 0;
permutation = permutations [x];
if (permutation [0].from != "e") continue;
result = "e";
for (var y = 0; y < permutation.length; y ++) {
var possible = indexes (result, permutation [y].from);
if (possible.length == 0) break;
possible = possible.reverse ();
for (var z = 0; z < possible.length; z ++) {
result = result.replaceAt (possible [z], permutation [y].from, permutation [y].to);
steps ++;
if (result == objective) {
if (steps < least) least = steps;
break;
}
}
}
console.log ((x + 1) + "/" + permutations.length);
}
console.log (least);
alert (least);