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
175
176
|
let dict, word1, word2, ladder;
const ourParagraph = document.getElementById('ourParagraph');
const ourParagraph2 = document.getElementById('ourParagraph2');
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', handleFileUpload);
const ourWord = document.getElementById('ourWord');
ourWord.addEventListener('change', onWordChange);
const ourWord2 = document.getElementById('ourWord2');
ourWord2.addEventListener('change', onWord2Change);
function onWordChange(event) {
word1 = event.target.value;
display();
}
function onWord2Change(event) {
word2 = event.target.value;
display();
}
function handleFileUpload(event) {
const file = event.target.files[0];
if (!file) {
console.error('No file selected');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
const words = content.split(/\s+/);
dict = words;
getLadder();
display2();
};
reader.readAsText(file);
}
function quit() {
console.log("Output: ()");
}
function neighbor(word, word_list) {
for (let w of word_list)
if (dist(w, word) == 1)
return w;
}
function dist(wrd1, wrd2) {
let cnt = 0;
let w1 = wrd1.split('');
let w2 = wrd2.split('');
for (let i = 0; i < w1.length; i++)
if (w1[i] != w2[i])
cnt++;
return cnt;
}
function check(m, n, A, B) {
let ans = [];
for (let i = 0; i <= m; i++)
for (let j = 0; j <= n; j++){
let a = intersection(A[i], B[j]);
if (a.length > 0)
for (let e of a)
ans.push(e);
}
return ans;
}
function expand(word, words) {
let new1 = [];
for (let i = 0; i < word.length; i++) {
let pre = word.slice(0, i);
let post = word.slice(i+1);
for (let w of words) {
let pat = new RegExp(pre + "." + post);
if (pat.test(w))
new1.push(w);
}
}
let anew = new1; // filter for not eq to word
return anew;
}
function intersection(A, B) {
let ints = [];
for (let i = 0; i < A.length; i++)
for (let j = 0; j < B.length; j++)
if (A[i] == B[j])
ints.push(A[i]);
return ints;
}
function proc(input1, input2) {
let limit = 4;
console.log("Input:", input1, input2);
if (input1.length != input2.length)
return quit();
let cnt = 0;
for (let word of dict){
let reg1 = new RegExp(input1);
let reg2 = new RegExp(input2);
if (reg1.test(word) || reg2.test(word))
cnt++;
}
if (cnt < 2)
return quit();
let wordlen = input1.length;
let words = []
for (let d of dict)
if (d.length == wordlen)
words.push(d);
let A = [[input1]]
let B = [[input2]]
let center = []
let lvl = 0;
while (lvl < limit && center.length == 0) {
let anew = [];
let bnew = [];
for (let wa of A[lvl])
for (let w of expand(wa, words))
anew.push(w);
for (let wb of B[lvl])
for (let w of expand(wb, words))
bnew.push(w);
let uniq_a = [];
let uniq_b = [];
for (let item of anew)
if (!(item in uniq_a))
uniq_a.push(item);
for (let item of bnew)
if (!(item in uniq_b))
uniq_b.push(item);
A.push(uniq_a);
B.push(uniq_b);
lvl++;
center = check(lvl, lvl, A, B);
}
if (lvl == limit)
return quit();
let counter = lvl - 1;
let mylist = [center[0]];
let x = neighbor(center[0], A[counter]);
let y = neighbor(center[0], B[counter]);
if (y != null) {
while (counter > 0) {
mylist.unshift(x);
mylist.push(y);
counter--;
x = neighbor(x, A[counter]);
y = neighbor(y, B[counter]);
}
}
if (x != null)
mylist.unshift(x);
if (y != null)
mylist.push(y);
console.log("Output:", mylist);
}
function getLadder() {
console.log("dictionary loaded");
proc(word1, word2);
}
function display() {
ourParagraph.innerHTML = `word1: ${word1}<br>word2: ${word2}<br>`;
}
function display2() {
ourParagraph2.innerHTML = `${dict}`;
}
|