-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle2b.js
More file actions
80 lines (70 loc) · 2.41 KB
/
puzzle2b.js
File metadata and controls
80 lines (70 loc) · 2.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const input = require('./input/puzzle2a_input.json');
// uses levenshtein distance to see how different the stings are.
// more complex, still fast, but allows for more use cases like if the strings
// are different lengths.
function LevenshteinDistance(string1, string2) {
// using the Wagner-Fischer algorithm
// set each element in distance to 0
const distance = getDistanceMatrix(string1, string2);
for (let j = 1; j <= string1.length; j++) {
for (let k = 1; k <= string2.length; k++) {
if (string1[k] == string2[j]) {
distance[j][k] = distance[j-1][k-1]
} else{
distance[j][k] = Math.min(distance[j-1][k] + 1, // a deletion
distance[j][k-1] + 1, // an insertion
distance[j-1][k-1] + 1 // a substitution
);
}
}
}
return distance[string1.length][string2.length];
}
function getDistanceMatrix(s1, s2) {
const d = []
for (let row = 0; row <= s1.length; row++) {
d[row] = []
for (let col = 0; col <= s2.length; col++) {
if (row > 0 && col == 0) {
d[row][col] = row;
}else if (row == 0 && col > 0){
d[row][col] = col;
} else{
d[row][col] = 0
}
}
}
return d;
}
// simple solution that works for this data set
// only works if strings are same length
function getCommonLetters(s1, s2) {
let result_string = ""
for (let i = 0; i < s1.length; i++) {
if (s1[i] == s2[i]) {
result_string += s1[i];
}
}
return result_string;
}
function puzzle2b_simple(input) {
for (let i = 0; i < input.length; i++) {
for (let n = 0; n < input.length; n++) {
const letters = getCommonLetters(input[i], input[n]);
if (letters.length == input[i].length -1){
return letters;
}
}
}
}
function puzzle2b_levenshtein(input) {
for (let i = 0; i < input.length; i++) {
for (let n = 0; n < input.length; n++) {
if (LevenshteinDistance(input[i], input[n]) == 1){
return getCommonLetters(input[i], input[n]);
}
}
}
}
console.log(puzzle2b_simple(input));
console.log(puzzle2b_levenshtein(input));