part 4 complete
This commit is contained in:
parent
f902e5f6b3
commit
2dd81df725
2 changed files with 33 additions and 25 deletions
56
4/index.js
56
4/index.js
|
|
@ -1,6 +1,6 @@
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const forwardRegex = /XMAS/g;
|
const forwardRegex = /XMAS/;
|
||||||
const backwardRegex = /SAMX/g;
|
const backwardRegex = /SAMX/;
|
||||||
const input = fs.readFileSync("input.txt", "utf-8");
|
const input = fs.readFileSync("input.txt", "utf-8");
|
||||||
const inputArr = input.split("\n");
|
const inputArr = input.split("\n");
|
||||||
let totalMatches = 0;
|
let totalMatches = 0;
|
||||||
|
|
@ -10,18 +10,21 @@ let totalMatches = 0;
|
||||||
// console.log(forwardMatchs.length)
|
// console.log(forwardMatchs.length)
|
||||||
//create a sliding window to match the words
|
//create a sliding window to match the words
|
||||||
|
|
||||||
// for (row of inputArr) {
|
let lineRegex = 0;
|
||||||
// // console.log(row)
|
for (row of inputArr) {
|
||||||
// const forwardMatches = row.match(forwardRegex);
|
// console.log(row)
|
||||||
// const backwordsMatches = row.match(backwardRegex);
|
const forwardMatches = row.match(forwardRegex);
|
||||||
|
const backwordsMatches = row.match(backwardRegex);
|
||||||
|
|
||||||
// if (forwardMatches) {
|
if (forwardMatches) {
|
||||||
// totalMatches += forwardMatches.length;
|
lineRegex += forwardMatches.length;
|
||||||
// }
|
}
|
||||||
// if (backwordsMatches) {
|
if (backwordsMatches) {
|
||||||
// totalMatches += backwordsMatches.length;
|
lineRegex += backwordsMatches.length;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
console.log("lineRegex: ",lineRegex)
|
||||||
|
|
||||||
// console.log(inputArr)
|
// console.log(inputArr)
|
||||||
const maxRows = inputArr.length;
|
const maxRows = inputArr.length;
|
||||||
|
|
@ -29,41 +32,46 @@ const maxLetters = inputArr[0].length;
|
||||||
|
|
||||||
//horizontal words
|
//horizontal words
|
||||||
for (let x = 0; x < maxRows; x++) {
|
for (let x = 0; x < maxRows; x++) {
|
||||||
for (let y = 0; y + 3 <= maxLetters; y++) {
|
for (let y = 0; y + 3 < maxLetters; y++) {
|
||||||
const word = inputArr[x][y] + inputArr[x][y + 1] + inputArr[x][y + 2] + inputArr[x][y + 3];
|
const word = inputArr[x][y] + inputArr[x][y + 1] + inputArr[x][y + 2] + inputArr[x][y + 3];
|
||||||
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
// console.log(word)
|
||||||
|
if (word.match(forwardRegex)|| word.match(backwardRegex)) {
|
||||||
totalMatches++;
|
totalMatches++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("after horizontal: ",totalMatches)
|
||||||
|
|
||||||
//vertical words
|
//vertical words
|
||||||
for (let x = 0; x + 3 < maxRows; x++) {
|
for (let x = 0; x + 3 < maxRows; x++) {
|
||||||
for (let y = 0; y <= maxLetters; y++) {
|
for (let y = 0; y < maxLetters; y++) {
|
||||||
const word = inputArr[x][y] + inputArr[x + 1][y] + inputArr[x + 2][y] + inputArr[x + 3][y];
|
const word = inputArr[x][y] + inputArr[x + 1][y] + inputArr[x + 2][y] + inputArr[x + 3][y];
|
||||||
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
if (word.match(forwardRegex)|| word.match(backwardRegex)) {
|
||||||
totalMatches++;
|
totalMatches++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//angled words 1
|
console.log("after vertical: ",totalMatches)
|
||||||
for (let x = 0; x + 4 < maxRows; x++) {
|
//angled words nw-se
|
||||||
|
for (let x = 0; x + 3 < maxRows; x++) {
|
||||||
for (let y = 0; y +3< maxLetters; y++) {
|
for (let y = 0; y +3< maxLetters; y++) {
|
||||||
const word = inputArr[x][y] + inputArr[x + 1][y + 1] + inputArr[x + 2][y + 2] + inputArr[x + 3][y + 3];
|
const word = inputArr[x][y] + inputArr[x + 1][y + 1] + inputArr[x + 2][y + 2] + inputArr[x + 3][y + 3];
|
||||||
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
if (word.match(forwardRegex)|| word.match(backwardRegex)) {
|
||||||
totalMatches++;
|
totalMatches++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//angled words 2
|
console.log("after nw-se: ",totalMatches)
|
||||||
for (let x = 0; x + 4 < maxRows; x++) {
|
//angled words ne-sw
|
||||||
|
for (let x = 0; x + 3 < maxRows; x++) {
|
||||||
for (let y = 0; y +3< maxLetters; y++) {
|
for (let y = 0; y +3< maxLetters; y++) {
|
||||||
const word = inputArr[x][y+3] + inputArr[x+1][y + 2] + inputArr[x + 2][y + 1] + inputArr[x+3][y];
|
const word = inputArr[x][y+3] + inputArr[x+1][y + 2] + inputArr[x + 2][y + 1] + inputArr[x+3][y];
|
||||||
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
if (word.match(forwardRegex)|| word.match(backwardRegex)) {
|
||||||
totalMatches++;
|
totalMatches++;
|
||||||
}
|
}
|
||||||
console.log(word);
|
// console.log(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log("after ne-sw: ",totalMatches)
|
||||||
|
|
||||||
console.log("totalMatches: ", totalMatches);
|
console.log("totalMatches: ", totalMatches);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue