complete but wrong answer
This commit is contained in:
parent
cddf77d09b
commit
89c13d8f38
1 changed files with 58 additions and 16 deletions
70
4/index.js
70
4/index.js
|
|
@ -1,8 +1,8 @@
|
||||||
const fs = require("fs")
|
const fs = require("fs");
|
||||||
const forwardRegex = /XMAS/g
|
const forwardRegex = /XMAS/g;
|
||||||
const backwardRegex = /SAMX/g
|
const backwardRegex = /SAMX/g;
|
||||||
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;
|
||||||
// console.log(inputArr)
|
// console.log(inputArr)
|
||||||
// const forwardMatchs = input.match(forwardRegex)
|
// const forwardMatchs = input.match(forwardRegex)
|
||||||
|
|
@ -10,18 +10,60 @@ 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) {
|
||||||
|
// // console.log(row)
|
||||||
|
// const forwardMatches = row.match(forwardRegex);
|
||||||
|
// const backwordsMatches = row.match(backwardRegex);
|
||||||
|
|
||||||
for (row of inputArr){
|
// if (forwardMatches) {
|
||||||
// console.log(row)
|
// totalMatches += forwardMatches.length;
|
||||||
const forwardMatches = row.match(forwardRegex)
|
// }
|
||||||
const backwordsMatches= row.match(backwardRegex)
|
// if (backwordsMatches) {
|
||||||
|
// totalMatches += backwordsMatches.length;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
if(forwardMatches){
|
// console.log(inputArr)
|
||||||
totalMatches += forwardMatches.length
|
const maxRows = inputArr.length;
|
||||||
|
const maxLetters = inputArr[0].length;
|
||||||
|
|
||||||
|
//horizontal words
|
||||||
|
for (let x = 0; x < maxRows; x++) {
|
||||||
|
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];
|
||||||
|
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
||||||
|
totalMatches++;
|
||||||
}
|
}
|
||||||
if(backwordsMatches){
|
|
||||||
totalMatches += backwordsMatches.length
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("totalMatches: ",totalMatches)
|
//vertical words
|
||||||
|
for (let x = 0; x + 3 < maxRows; x++) {
|
||||||
|
for (let y = 0; y <= maxLetters; 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)) {
|
||||||
|
totalMatches++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//angled words 1
|
||||||
|
for (let x = 0; x + 4 < maxRows; x++) {
|
||||||
|
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];
|
||||||
|
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
||||||
|
totalMatches++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//angled words 2
|
||||||
|
for (let x = 0; x + 4 < maxRows; x++) {
|
||||||
|
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];
|
||||||
|
if (forwardRegex.test(word) || backwardRegex.test(word)) {
|
||||||
|
totalMatches++;
|
||||||
|
}
|
||||||
|
console.log(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("totalMatches: ", totalMatches);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue