From 2dd81df72595d049ec1d67da609123466a60713e Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 28 Apr 2025 23:19:46 +0000 Subject: [PATCH] part 4 complete --- 4/index.js | 56 ++++++++++++++++++++++++++++++----------------------- 4/input.txt | 2 +- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/4/index.js b/4/index.js index be7b968..01e06e4 100644 --- a/4/index.js +++ b/4/index.js @@ -1,6 +1,6 @@ const fs = require("fs"); -const forwardRegex = /XMAS/g; -const backwardRegex = /SAMX/g; +const forwardRegex = /XMAS/; +const backwardRegex = /SAMX/; const input = fs.readFileSync("input.txt", "utf-8"); const inputArr = input.split("\n"); let totalMatches = 0; @@ -10,18 +10,21 @@ let totalMatches = 0; // 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); +let lineRegex = 0; +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; -// } -// } + if (forwardMatches) { + lineRegex += forwardMatches.length; + } + if (backwordsMatches) { + lineRegex += backwordsMatches.length; + } +} + +console.log("lineRegex: ",lineRegex) // console.log(inputArr) const maxRows = inputArr.length; @@ -29,41 +32,46 @@ const maxLetters = inputArr[0].length; //horizontal words 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]; - if (forwardRegex.test(word) || backwardRegex.test(word)) { + // console.log(word) + if (word.match(forwardRegex)|| word.match(backwardRegex)) { totalMatches++; } } } +console.log("after horizontal: ",totalMatches) //vertical words 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]; - if (forwardRegex.test(word) || backwardRegex.test(word)) { + if (word.match(forwardRegex)|| word.match(backwardRegex)) { totalMatches++; } } } -//angled words 1 -for (let x = 0; x + 4 < maxRows; x++) { +console.log("after vertical: ",totalMatches) +//angled words nw-se +for (let x = 0; x + 3 < 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)) { + if (word.match(forwardRegex)|| word.match(backwardRegex)) { totalMatches++; } } } -//angled words 2 -for (let x = 0; x + 4 < maxRows; x++) { +console.log("after nw-se: ",totalMatches) +//angled words ne-sw +for (let x = 0; x + 3 < 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)) { + if (word.match(forwardRegex)|| word.match(backwardRegex)) { totalMatches++; } - console.log(word); + // console.log(word); } } +console.log("after ne-sw: ",totalMatches) console.log("totalMatches: ", totalMatches); diff --git a/4/input.txt b/4/input.txt index 45af8b4..d8ec064 100644 --- a/4/input.txt +++ b/4/input.txt @@ -137,4 +137,4 @@ MAAMMXSAMSSXSASXSXSSXAMASXMSMSSSMASAXXSMAMSMMMSAAXAASMSSMMSXMASAMAMAMAXAAAAMMMSM MMXMXSSXMAMXMASMSAAXXMMASXMAAMAAMMMMMAXMSXMASAMXSMMSSXAXSXAXMXMAMXXXSSMSMSMSAMXMSXMMSMMXSAMMSSXMSMSSMSMXXAAASMMXMXAXXXXXMXXMASMMSAAAAXSXSSSM MSASXAMXMASXSXSAMMMMSXMASASMSMSMMSAMMSMAMMMAMASMXMMMMMMMMMMMMSSXMMMXMAMXMAXMASMAXXASAMAMMMMMAMXAAAAMAMXSMSSMMXXAXMMSMXMMSAXSAMAMMMMMSMMAAAXX ASASMXMASMMASAMAMXXASAMASMMAAAAAMMAMAAMXMAMXSAMAAXMAAAAAAAXAAASAAAXASAMAMAXXAAMAMSSXMAXSAAMMASMSMMMMXMAXMXAAXSSMSAASMSXAAAXMASMMAAAAAAMSMMMA -XMXMASMXSXMSMXSSMAMXSXMXSXMSMSMSXSSMSXSASXSAMXXSASXSSSSSSSSMSSSSMMSASMSXSMXMASMAMSASXAXSMSSSSMMAXSMXSMXSMSSMMAXASMXMASMXMSMSAMMSXSMSSSMXXXSX +XMXMASMXSXMSMXSSMAMXSXMXSXMSMSMSXSSMSXSASXSAMXXSASXSSSSSSSSMSSSSMMSASMSXSMXMASMAMSASXAXSMSSSSMMAXSMXSMXSMSSMMAXASMXMASMXMSMSAMMSXSMSSSMXXXSX \ No newline at end of file