-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.html
187 lines (176 loc) · 6.63 KB
/
check.html
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
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(function(window) {
const HOP = 1;
const DIRECTIONS = [
{ dx: -HOP, dy: 0 },
{ dx: +HOP, dy: 0 },
{ dx: 0, dy: -HOP },
{ dx: 0, dy: +HOP },
];
let el = {};
let matrix = [];
let width = 0;
let height = 0;
function traverseTree(x, y, tail, path) {
if (matrix[x][y] !== tail[0]) {
return false;
}
path.push({ x: x, y: y });
tail.shift();
if (tail.length === 0) {
return true;
}
let hasPath = false;
for (const d of DIRECTIONS) {
const { dx, dy } = d;
const i = (x + dx + width) % width;
const j = (y + dy + height) % height;
hasPath ||= traverseTree(i, j, [...tail], path);
}
if (hasPath) {
return true;
}
path.pop();
return false;
}
function getWordPaths(word) {
word = word.split('');
const paths = [];
for (let x = 0; x < width; ++x) {
for (let y = 0; y < height; ++y) {
const path = []
traverseTree(x, y, [...word], path);
if (path.length === word.length) {
paths.push(path);
}
}
}
return paths;
}
function clearHighlights() {
for (const c of el.matrix.children) {
c.classList.remove('highlight');
}
}
function updateHighlighting(word) {
clearHighlights();
const paths = getWordPaths(word);
for (const path of paths) {
for (const coord of path) {
const cell = document.querySelector(`#cell-${coord.x}-${coord.y}`);
cell.className = 'highlight';
}
}
}
function onWordChange(e) {
updateHighlighting(e.target.value.toUpperCase());
}
function transposed(m) {
return m[0].map((x, i) => m.map(x => x[i]));
}
function buildMatrix() {
const m = el.pasted.value.replace(/ /g,'').split('\n').map(row => row.trim().toUpperCase().split(''));
width = m[0].length;
height = m.length;
matrix = transposed(m);
const children = [];
for (let y = 0; y < height; ++y) {
for (let x = 0; x < width; ++x) {
const cell = document.createElement('div');
cell.textContent = matrix[x][y];
cell.id = `cell-${x}-${y}`;
children.push(cell);
}
}
el.matrix.style.gridTemplateColumns = 'min-content '.repeat(width);
el.matrix.style.gridTemplateRows = 'min-content '.repeat(height);
el.matrix.replaceChildren(...children);
updateHighlighting(el.word.value);
}
function main() {
el.word = document.querySelector('#word');
el.word.addEventListener('input', onWordChange);
el.word.focus();
el.pasted = document.querySelector('#pasted');
el.pasted.addEventListener('input', () => {
buildMatrix();
el.word.focus();
});
el.matrix = document.querySelector('#matrix');
buildMatrix();
}
window.addEventListener('load', main);
})(window);
</script>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
:root {
--bg-color: #181818;
--grid-color: #333;
--fg-color: #e8e8e8;
--field-color: #f8f8f8;
--highlight-color: rgba(11, 226, 22, 0.723);
}
html {
background-color: var(--bg-color);
color: var(--fg-color);
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-size: 11pt;
}
div {
margin: 5px auto 0 5px;
}
input, textarea {
background-color: var(--field-color);
border: 1px solid var(--bg-color);
padding: 5px;
}
input {
min-width: 20em;
}
textarea {
min-width: 24em;
min-height: 40ex;
letter-spacing: 0.5em;
}
#word {
text-transform: uppercase;
font-family: monospace;
}
#matrix {
display: grid;
margin-bottom: 1ex;
padding: 5px 0;
}
#matrix > div {
text-align: center;
font-family: monospace;
width: 22px;
height: 22px;
margin: 0;
border: 1px solid var(--grid-color);
}
.highlight {
background-color: var(--highlight-color);
}
</style>
</head>
<body>
<div>
<input type="text" name="word" id="word" autocomplete="off" placeholder="Type word to find ..." />
</div>
<div id="matrix"></div>
<div>
<textarea id="pasted" placeholder="Paste matrix here ..."></textarea>
</div>
</body>
</html>