From 5764869e99ade916d2a7c98af7d5953fdae86d99 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Wed, 4 Dec 2024 23:05:28 -0500 Subject: [PATCH] much cleaner way to look for shapes in grids --- .aocrunner.json | 6 +-- src/day04/index.ts | 126 +++++++++------------------------------------ 2 files changed, 28 insertions(+), 104 deletions(-) diff --git a/.aocrunner.json b/.aocrunner.json index eeaeccf..9b9c941 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -48,17 +48,17 @@ { "part1": { "solved": true, - "result": "2358", + "result": "2143", "attempts": [ "0" ], - "time": 327.70272 + "time": 67.709144 }, "part2": { "solved": true, "result": "1737", "attempts": [], - "time": 201.22507 + "time": 182.986319 } }, { diff --git a/src/day04/index.ts b/src/day04/index.ts index 872ca5c..0d9bdf4 100644 --- a/src/day04/index.ts +++ b/src/day04/index.ts @@ -4,118 +4,42 @@ const parseInput = (rawInput: string) => { return rawInput.split('\n').map(line => line.split('')) } -const directions = ["down","right","dl","dr"] as const -type Direction = typeof directions[number] -type Prospect1 = { - letters: string - direction: Direction - next: [number, number] -} -type Coord = Prospect1['next'] -const getNext = (dir: Direction, [row, col]: Coord): Coord => { - switch(dir) { - case "down": {return [row+1, col]} - case "right": {return [row, col+1]} - case "dl": {return [row+1, col-1]} - case "dr": {return [row+1, col+1]} - } -} - const part1 = (rawInput: string) => { const input = parseInput(rawInput) let total = 0 - let prospects: Prospect1[] = [] - input.forEach((line, row) => { - line.forEach((letter, col) => { - prospects = prospects.map((prospect) => { - const { - direction, - letters, - next - } = prospect - if (next[0] < row) return null - if (next[0] === row && next[1] === col) { - const nextLetters = letters + letter - if (["XMAS","SAMX"].includes(nextLetters)) { - total += 1 - return null - } - if (["XMA","SAM","XM","SA"].includes(nextLetters)) { - return { - letters: nextLetters, - direction: direction, - next: getNext(direction, next) - } - } - } - return prospect - }).filter(prospect => prospect != null) - if ("XMAS".includes(letter)) { - directions.forEach(direction => { - prospects.push({ - letters: letter, - direction, - next: getNext(direction, [row, col]) - }) - }) - } + input.forEach((line,r) => { + line.forEach((cell, c) => { + total += [ + [[r,c],[r,c+1],[r,c+2],[r,c+3]], // right + [[r,c],[r+1,c+1],[r+2,c+2],[r+3,c+3]], // downright + [[r,c],[r+1,c],[r+2,c],[r+3,c]], // down + [[r,c],[r+1,c-1],[r+2,c-2],[r+3,c-3]], // downleft + ].reduce((acc,cur) => /XMAS|SAMX/.test(cur.map(([r,c]) => { + return (input[r] ?? [])[c] ?? "." + }).join('')) ? acc+1:acc, 0) }) }) return total } -type Prospect2 = { - letters: string - direction: Direction - next: [number, number] - center: [number, number] -} - const part2 = (rawInput: string) => { const input = parseInput(rawInput) - let centers = new Map() - let prospects: Prospect2[] = [] let total = 0 - input.forEach((line, row) => { - line.forEach((letter, col) => { - prospects = prospects.map((prospect) => { - const { - direction, - letters, - next - } = prospect - if (next[0] < row) return null - if (next[0] === row && next[1] === col) { - const nextLetters = letters + letter - if (["MAS","SAM"].includes(nextLetters)) { - const key = prospect.center.join(",") - if (centers.has(key)) { total++ } - centers.set(key, 1) - return null - } - if (["MA","SA"].includes(nextLetters)) { - return { - ...prospect, - letters: nextLetters, - next: getNext(direction, next), - } - } - } - return prospect - }).filter(prospect => prospect != null) - if ("XMAS".includes(letter)) { - ["dl" as const, "dr" as const].forEach(direction => { - const center = getNext(direction, [row, col]) - prospects.push({ - letters: letter, - direction, - next: center, - center, - }) - }) + for (let r = 0; r < input.length -2; r++) { + for (let c = 0; c < input[0].length -2; c++) { + if(/MMASS|SSAMM|MSAMS|SMASM/.test( + [ + [r,c], + [r,c+2], + [r+1,c+1], + [r+2,c], + [r+2,c+2] + ].map(([r,c]) => input[r][c]).join('') + )) { + total++ } - }) - }) + } + } return total } @@ -165,5 +89,5 @@ MXMXAXMASX`, solution: part2, }, trimTestInputs: true, - onlyTests: false, + onlyTests: true, })