diff --git a/4/indexP2.js b/4/indexP2.js new file mode 100644 index 0000000..33f11be --- /dev/null +++ b/4/indexP2.js @@ -0,0 +1,44 @@ +const fs = require("fs"); +const forwardRegex = /XMAS/; +const backwardRegex = /SAMX/; +const input = fs.readFileSync("input.txt", "utf-8"); +const inputArr = input.split("\n"); +let totalMatches = 0; + +const middleARegex = /.A./ +const MMRegex=/M.M/ +const SSRegex=/S.S/ +const MSRegex=/M.S/ +const SMRegex=/S.M/ + + +// console.log(inputArr) +const maxRows = inputArr.length; +const maxLetters = inputArr[0].length; + + +for (let x = 0; x+2 < maxRows; x++) { + for (let y = 0; y + 2 < maxLetters; y++) { + const topLine = inputArr[x][y]+inputArr[x][y+1]+inputArr[x][y+2] + const middleLine= inputArr[x+1][y]+inputArr[x+1][y+1]+inputArr[x+1][y+2] + const bottomLIne=inputArr[x+2][y]+inputArr[x+2][y+1]+inputArr[x+2][y+2] + + if (topLine.match(MMRegex)&&middleLine.match(middleARegex)&&bottomLIne.match(SSRegex)){ + totalMatches++ + } + + if (topLine.match(SSRegex)&&middleLine.match(middleARegex)&&bottomLIne.match(MMRegex)){ + totalMatches++ + } + + if (topLine.match(MSRegex)&&middleLine.match(middleARegex)&&bottomLIne.match(MSRegex)){ + totalMatches++ + } + if (topLine.match(SMRegex)&&middleLine.match(middleARegex)&&bottomLIne.match(SMRegex)){ + totalMatches++ + } + } +} + + +console.log("totalMatches: ", totalMatches);