From cddf77d09bac784d3cab11c692adfdfa64db5207 Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 28 Apr 2025 08:54:41 +0000 Subject: [PATCH] forward and backmatches counted --- 4/index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/4/index.js b/4/index.js index cb27f22..e9b96f8 100644 --- a/4/index.js +++ b/4/index.js @@ -1,9 +1,27 @@ const fs = require("fs") const forwardRegex = /XMAS/g +const backwardRegex = /SAMX/g const input = fs.readFileSync("input.txt","utf-8") const inputArr = input.split("\n") -console.log(inputArr) +let totalMatches= 0; +// console.log(inputArr) // const forwardMatchs = input.match(forwardRegex) // console.log(input) // console.log(forwardMatchs.length) //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) + + if(forwardMatches){ + totalMatches += forwardMatches.length + } + if(backwordsMatches){ + totalMatches += backwordsMatches.length + } +} + +console.log("totalMatches: ",totalMatches) \ No newline at end of file