AdventCalendars/KnowItJulekalender/2020/03/find-words.js
Alex Thomassen 0cb5a45aa2
[KnowIt] 2020/04 - Add completed solution
[KnowIt] 2020/03 - Add incomplete solution (missing diagonals)
2020-12-04 13:48:24 +01:00

82 lines
1.8 KiB
JavaScript

/**
* This was started on, but never completed.
* me not smart enuff 4 dis :(
*
* I think the only thing really missing is checking diagonally, maybe?
*/
const { readFile } = require('../../../helpers');
const file = readFile('./test-matrix.txt').trim();
const words = readFile('./test-wordlist.txt')
.trim()
.split('\n')
.sort();
const strings = {
horizontal: [],
horizontalReverse: [],
vertical: [],
verticalReverse: [],
};
function reverseString(s) {
/**
* JS does not have a `.reverse()` method for strings
* so we split into an array and re-join into a string.
*/
return s.split('')
.reverse()
.join('');
}
strings.horizontal = file.split('\n');
strings.horizontalReverse = strings.horizontal.map(reverseString);
/**
* Find all "vertical" strings
*/
for (const strIdx in strings.horizontal)
{
const str = strings.horizontal[strIdx];
for (const letterIdx in str)
{
/**
* Make sure the entry is a string so we can string concat.
*/
if (typeof strings.vertical[letterIdx] !== 'string') {
strings.vertical[letterIdx] = '';
}
const letter = str[letterIdx];
strings.vertical[letterIdx] += letter;
}
}
strings.verticalReverse = strings.vertical.map(reverseString);
const found = [];
for (const type in strings)
{
const strs = strings[type];
console.log(`Checking ${type} strings...`);
for (const str of strs)
{
for (const word of words)
{
if (!str.includes(word)) {
continue;
}
if (found.includes(word)) {
continue;
}
found.push(word);
}
}
}
// const results = words.filter(s => !found.includes(s));
// console.log(results);