|  | 
|  | 1 | +export type CharacterArray = string[][] | 
|  | 2 | + | 
|  | 3 | +interface CoordinateBase { | 
|  | 4 | +  x: number; | 
|  | 5 | +  y: number; | 
|  | 6 | +} | 
|  | 7 | + | 
|  | 8 | +interface Coordinate extends CoordinateBase { | 
|  | 9 | +  direction: 1 | -1; | 
|  | 10 | +} | 
|  | 11 | + | 
|  | 12 | +interface CoordinateData { | 
|  | 13 | +  xDirection: 1 | -1; | 
|  | 14 | +  yDirection: 1 | -1; | 
|  | 15 | +  data: CharacterArray | 
|  | 16 | +} | 
|  | 17 | + | 
|  | 18 | +/** | 
|  | 19 | + * Checks for the 'XMAS' word in the vertical directions (up/down) from an (x,y) coordinate | 
|  | 20 | + * @typedef coord {Coordinate} Coordinate object | 
|  | 21 | + * @param coord.x {number} x coordinate (array x index) | 
|  | 22 | + * @param coord.y {number} y coordinate (array y index) | 
|  | 23 | + * @param coord.direction {number} Up or down letter-checking direction | 
|  | 24 | + *   - `-1` for "up" going to (0,0) | 
|  | 25 | + *   - `1` for "down" going to (N,N) from the `coord` | 
|  | 26 | + * @param fullData {CharacterArray} 2D array of strings consisting of letters per item | 
|  | 27 | + * @param WORD {string} Word to find | 
|  | 28 | + * @returns {boolean} Flag indicating the existence of the 'XMAS' word | 
|  | 29 | + */ | 
|  | 30 | +export const checkVertical = (coord: Coordinate, fullData: CharacterArray, WORD: string): boolean => { | 
|  | 31 | +  let yIndex = coord.y | 
|  | 32 | +  let subWord = '' | 
|  | 33 | + | 
|  | 34 | +  if (!Array.isArray(fullData)) return false | 
|  | 35 | + | 
|  | 36 | +  for (let i = 0; i < WORD.length; i += 1) { | 
|  | 37 | +    if (yIndex < 0) break | 
|  | 38 | +    if (yIndex >= fullData.length) break | 
|  | 39 | + | 
|  | 40 | +    subWord += (fullData[yIndex])?.[coord.x] | 
|  | 41 | +    yIndex += (coord.direction * 1) | 
|  | 42 | +  } | 
|  | 43 | + | 
|  | 44 | +  return subWord === WORD | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +/** | 
|  | 48 | + * Checks for the 'XMAS' word in the horizontal directions (left/right) from an (x,y) coordinate | 
|  | 49 | + * @typedef coord {Coordinate} Coordinate object | 
|  | 50 | + * @param coord.x {number} x coordinate (array x index) | 
|  | 51 | + * @param coord.y {number} y coordinate (array y index) | 
|  | 52 | + * @param coord.direction {number} `-1` for "left" going to (0,0) or `1` for "right" going to (N,N) from the `coord` | 
|  | 53 | + * @param rowData {string[]} Array of row-wise letters from a 2D array | 
|  | 54 | + * @returns {boolean} Flag indicating the existence of the 'XMAS' word | 
|  | 55 | + * @param WORD {string} Word to find | 
|  | 56 | + */ | 
|  | 57 | +export const checkHorizontal = (coord: Coordinate, rowData: string[], WORD: string): boolean => { | 
|  | 58 | +  let xIndex = coord.x | 
|  | 59 | +  let subWord = '' | 
|  | 60 | + | 
|  | 61 | +  if (!Array.isArray(rowData)) return false | 
|  | 62 | + | 
|  | 63 | +  for (let i = 0; i < WORD.length; i += 1) { | 
|  | 64 | +    if (xIndex < 0) break | 
|  | 65 | +    if (xIndex >= rowData.length) break | 
|  | 66 | + | 
|  | 67 | +    subWord += rowData[xIndex] | 
|  | 68 | +    xIndex += (coord.direction * 1) | 
|  | 69 | +  } | 
|  | 70 | + | 
|  | 71 | +  return subWord === WORD | 
|  | 72 | +} | 
|  | 73 | + | 
|  | 74 | +/** | 
|  | 75 | + * Checks for the 'XMAS' word in the horizontal directions from an (x,y) coordinate | 
|  | 76 | + * @typedef coord {Coordinate} Coordinate object | 
|  | 77 | + * @param coord.x {number} x coordinate (array x index) | 
|  | 78 | + * @param coord.y {number} y coordinate (array y index) | 
|  | 79 | + * @typedef param {CoordinateData} Direction and data object | 
|  | 80 | + * @param param.xDirection {number} `-1` for "left" going to (0,0) or `1` for "right" going to (N,N) from the `coord` | 
|  | 81 | + * @param param.yDirection {number} `-1` for "up" going to (0,0) or `1` for "down" going to (N,N) from the `coord` | 
|  | 82 | + * @param param.data {CharacterArray} 2D string array containing the input text | 
|  | 83 | + * @returns {boolean} Flag indicating the existence of the 'XMAS' word | 
|  | 84 | + * @param WORD {string} Word to find | 
|  | 85 | + */ | 
|  | 86 | +export const checkDiagonal = (coord: CoordinateBase, param: CoordinateData, WORD: string): boolean => { | 
|  | 87 | +  let xIndex = coord.x | 
|  | 88 | +  let yIndex = coord.y | 
|  | 89 | +  let subWord = '' | 
|  | 90 | + | 
|  | 91 | +  if (!Array.isArray(param.data)) return false | 
|  | 92 | +  if (param.data[0] === undefined) return false | 
|  | 93 | + | 
|  | 94 | +  for (let i = 0; i < WORD.length; i += 1) { | 
|  | 95 | +    if (xIndex < 0 || xIndex >= param.data[0].length) break | 
|  | 96 | +    if (yIndex < 0 || yIndex >= param.data.length)  break | 
|  | 97 | + | 
|  | 98 | +    subWord += (param.data[yIndex])?.[xIndex] | 
|  | 99 | + | 
|  | 100 | +    xIndex += (param.xDirection * 1) | 
|  | 101 | +    yIndex += (param.yDirection * 1) | 
|  | 102 | +  } | 
|  | 103 | + | 
|  | 104 | +  return subWord === WORD | 
|  | 105 | +} | 
0 commit comments