|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +async function main() { |
| 5 | + const data = await readFile( |
| 6 | + resolve('../../../../', 'data', '2024-04.txt'), |
| 7 | + 'utf8' |
| 8 | + ); |
| 9 | + |
| 10 | + // Part 1 |
| 11 | + { |
| 12 | + console.time('Part 1'); |
| 13 | + const result = part1(data); |
| 14 | + console.info('Result:', result); |
| 15 | + console.timeEnd('Part 1'); |
| 16 | + } |
| 17 | + |
| 18 | + // Part 2 |
| 19 | + { |
| 20 | + console.time('Part 2'); |
| 21 | + const result = part2(data); |
| 22 | + console.info('Result:', result); |
| 23 | + console.timeEnd('Part 2'); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +main(); |
| 28 | + |
| 29 | +export function part1(data: string) { |
| 30 | + const matrix = data |
| 31 | + .trim() |
| 32 | + .split('\n') |
| 33 | + .map((line) => line.split('')); |
| 34 | + |
| 35 | + let sum = 0; |
| 36 | + |
| 37 | + for (let rowIdx = 0; rowIdx < matrix.length; rowIdx++) { |
| 38 | + for (let cellIdx = 0; cellIdx < matrix[rowIdx].length; cellIdx++) { |
| 39 | + const top = getTop(matrix, rowIdx, cellIdx).join(''); |
| 40 | + const left = getLeft(matrix, rowIdx, cellIdx).join(''); |
| 41 | + const topLeft = getTopLeft(matrix, rowIdx, cellIdx).join(''); |
| 42 | + const topRight = getTopRight(matrix, rowIdx, cellIdx).join(''); |
| 43 | + |
| 44 | + if (top === 'XMAS' || top === 'SAMX') { |
| 45 | + sum += 1; |
| 46 | + } |
| 47 | + if (left === 'XMAS' || left === 'SAMX') { |
| 48 | + sum += 1; |
| 49 | + } |
| 50 | + if (topLeft === 'XMAS' || topLeft === 'SAMX') { |
| 51 | + sum += 1; |
| 52 | + } |
| 53 | + if (topRight === 'XMAS' || topRight === 'SAMX') { |
| 54 | + sum += 1; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return sum; |
| 60 | +} |
| 61 | + |
| 62 | +export function part2(data: string) { |
| 63 | + const matrix = data |
| 64 | + .trim() |
| 65 | + .split('\n') |
| 66 | + .map((line) => line.split('')); |
| 67 | + |
| 68 | + let sum = 0; |
| 69 | + |
| 70 | + for (let rowIdx = 0; rowIdx < matrix.length; rowIdx++) { |
| 71 | + for (let cellIdx = 0; cellIdx < matrix[rowIdx].length; cellIdx++) { |
| 72 | + const c = matrix[rowIdx][cellIdx]; |
| 73 | + |
| 74 | + if (c === 'A') { |
| 75 | + const topLeft = matrix[rowIdx - 1]?.[cellIdx - 1]; |
| 76 | + const topRight = matrix[rowIdx - 1]?.[cellIdx + 1]; |
| 77 | + const bottomLeft = matrix[rowIdx + 1]?.[cellIdx - 1]; |
| 78 | + const bottomRight = matrix[rowIdx + 1]?.[cellIdx + 1]; |
| 79 | + |
| 80 | + const l = topLeft + c + bottomRight; |
| 81 | + const r = topRight + c + bottomLeft; |
| 82 | + |
| 83 | + if ((l === 'MAS' || l === 'SAM') && (r === 'MAS' || r === 'SAM')) { |
| 84 | + sum += 1; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return sum; |
| 91 | +} |
| 92 | + |
| 93 | +function getTop(matrix: string[][], x: number, y: number): string[] { |
| 94 | + return [ |
| 95 | + matrix?.[x]?.[y], |
| 96 | + matrix?.[x - 1]?.[y], |
| 97 | + matrix?.[x - 2]?.[y], |
| 98 | + matrix?.[x - 3]?.[y], |
| 99 | + ]; |
| 100 | +} |
| 101 | + |
| 102 | +function getLeft(matrix: string[][], x: number, y: number): string[] { |
| 103 | + return [ |
| 104 | + matrix?.[x]?.[y], |
| 105 | + matrix?.[x]?.[y - 1], |
| 106 | + matrix?.[x]?.[y - 2], |
| 107 | + matrix?.[x]?.[y - 3], |
| 108 | + ]; |
| 109 | +} |
| 110 | + |
| 111 | +function getTopLeft(matrix: string[][], x: number, y: number): string[] { |
| 112 | + return [ |
| 113 | + matrix?.[x]?.[y], |
| 114 | + matrix?.[x - 1]?.[y - 1], |
| 115 | + matrix?.[x - 2]?.[y - 2], |
| 116 | + matrix?.[x - 3]?.[y - 3], |
| 117 | + ]; |
| 118 | +} |
| 119 | + |
| 120 | +function getTopRight(matrix: string[][], x: number, y: number): string[] { |
| 121 | + return [ |
| 122 | + matrix?.[x]?.[y], |
| 123 | + matrix?.[x - 1]?.[y + 1], |
| 124 | + matrix?.[x - 2]?.[y + 2], |
| 125 | + matrix?.[x - 3]?.[y + 3], |
| 126 | + ]; |
| 127 | +} |
0 commit comments