From 3d139dfe423736fdffff3b6d3fcf01aad3a34652 Mon Sep 17 00:00:00 2001 From: TobenderZephyr Date: Mon, 6 Jan 2025 13:55:07 +0100 Subject: [PATCH] finished day 04 part 2 --- 2024/04/example.txt | 20 ++++++++-------- 2024/04/index.js | 57 +++++++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/2024/04/example.txt b/2024/04/example.txt index c41c5ea..a14c76e 100644 --- a/2024/04/example.txt +++ b/2024/04/example.txt @@ -1,10 +1,10 @@ -MMMSXXMASM -MSAMXMSMSA -AMXSXMAAMM -MSAMASMSMX -XMASAMXAMM -XXAMMXXAMA -SMSMSASXSS -SAXAMASAAA -MAMMMXMMMM -MXMXAXMASX \ No newline at end of file +.M.S...... +..A..MSMS. +.M.S.MAA.. +..A.ASMSM. +.M.S.M.... +.......... +S.S.S.S.S. +.A.A.A.A.. +M.M.M.M.M. +.......... \ No newline at end of file diff --git a/2024/04/index.js b/2024/04/index.js index 79fad91..6dcaa6f 100644 --- a/2024/04/index.js +++ b/2024/04/index.js @@ -10,10 +10,9 @@ for (let y = 0; y < data.length; y++) { } } - class Puzzle { - #directions = [] + #diagonals = [] constructor() { this.i = 0 this.solutions = 0 @@ -22,6 +21,9 @@ class Puzzle { [0, -1], [0, +1], [+1, -1], [+1, 0,], [+1, +1] ] + this.#diagonals = [ + [-1, -1], [-1, +1] + ] } shootInDirection(y, x, dy, dx, length) { const positions = [] @@ -39,19 +41,6 @@ class Puzzle { return [...paper.filter(character => character.char === c)] } - getDirections(x, y) { - return [...new Set([ - [Math.max(0, x - 1), Math.max(0, y - 1)], - [x, Math.max(0, y - 1)], - [Math.min(x + 1), Math.max(0, y - 1)], - [Math.max(0, x - 1), y], - [Math.min(x + 1), y], - [Math.max(x - 1), y + 1], - [x, Math.min(y + 1)], - [Math.min(x + 1), Math.min(y + 1)], - ])] - } - isInBoundary(x, y) { let max_width = data[0].length let max_height = data.length @@ -68,10 +57,21 @@ class Puzzle { } return true } + checkForWord2(d1, d2, word) { + const [y1, x1] = d1 + const [y2, x2] = d2 + if (!this.isInBoundary(x1, y1)) return false + if (!this.isInBoundary(x2, y2)) return false + const tl = this.isCharacterAt([...word][1], x1, y1) && this.isCharacterAt([...word][2], x2, y2) ? true : false + const tr = this.isCharacterAt([...word][2], x1, y1) && this.isCharacterAt([...word][1], x2, y2) ? true : false + return tl || tr + } + opposite(a) { + return -a + } solve(puzzle) { - console.log("--- solve ".padEnd(50, "-")) - const needed_length = puzzle.split('').length + console.log("--- solve part 1 ".padEnd(50, "-")) const word = puzzle.split('') const letter_positions = word.map(letter => { return { letter: letter, pos: this.findCharacter(letter).map(l => l.pos) } }) const starting_letter = letter_positions[0] @@ -86,7 +86,28 @@ class Puzzle { let total_words_possible = starting_letter.pos.length console.log(sum, "of possible", total_words_possible) } + + solve2(puzzle) { + console.log("--- solve part 2 ".padEnd(50, "-")) + const word = puzzle.split('') + const letter_positions = word.map(letter => { return { letter: letter, pos: this.findCharacter(letter).map(l => l.pos) } }) + const starting_letter = letter_positions[0] + let sum = 0 + for (let i = 0; i < starting_letter.pos.length; i++) { + let letters_in_both_dirs = true + for (let j = 0; j < this.#diagonals.length; j++) { + let [dy, dx] = this.#diagonals[j] + let upper = this.shootInDirection(starting_letter.pos[i].y, starting_letter.pos[i].x, dy, dx, (word.length - 1) / 2) + let lower = this.shootInDirection(starting_letter.pos[i].y, starting_letter.pos[i].x, this.opposite(dy), this.opposite(dx), (word.length - 1) / 2) + if (!this.checkForWord2(upper.flat(), lower.flat(), word)) letters_in_both_dirs = false + } + sum += letters_in_both_dirs ? 1 : 0 + } + let total_words_possible = starting_letter.pos.length + console.log(sum, "of possible", total_words_possible) + } } const p = new Puzzle() -p.solve("XMAS") \ No newline at end of file +p.solve("XMAS") +p.solve2("AMS") \ No newline at end of file