|  | 
|  | 1 | +import { GuardDirection } from '@/2024/2024-12-06/lib/guard.types.js' | 
|  | 2 | +import { getGridCoordinate } from '@/aoc/grid/utils.js' | 
|  | 3 | +import type { Point } from '@/aoc/point/types.js' | 
|  | 4 | + | 
|  | 5 | +/** | 
|  | 6 | + * @class Robot | 
|  | 7 | + * @description A set of methods, objects and variables for managing a `Robot` | 
|  | 8 | + */ | 
|  | 9 | +export class Robot { | 
|  | 10 | +  /** Symbol representation in the grid */ | 
|  | 11 | +  symbol: string = '@' | 
|  | 12 | + | 
|  | 13 | +  /** Current (y,x) grid coordinate */ | 
|  | 14 | +  pos: Point = { x: -1, y: -1 } | 
|  | 15 | + | 
|  | 16 | +  /** Direction indicator up/down/left/right */ | 
|  | 17 | +  direction: Point = { x: 0, y: 0 } | 
|  | 18 | + | 
|  | 19 | +  /** Current active move instruction symbol */ | 
|  | 20 | +  instruction: GuardDirection | '-' = '-' | 
|  | 21 | + | 
|  | 22 | +  /** Moves instructions set */ | 
|  | 23 | +  instructions: string[] | 
|  | 24 | + | 
|  | 25 | +  /** | 
|  | 26 | +   * @constructor | 
|  | 27 | +   * @param {string[][]} grid - 2D string array grid | 
|  | 28 | +   */ | 
|  | 29 | +  constructor (grid: string[][], instructions: string[]) { | 
|  | 30 | +    this.findInitialPosition(grid) | 
|  | 31 | +    this.instructions = [...instructions] | 
|  | 32 | +  } | 
|  | 33 | + | 
|  | 34 | +  /** | 
|  | 35 | +   * Finds the robot's initial `Point` position in the 2D grid, storing them in the `this.pos` object | 
|  | 36 | +   * @param {string[][]} grid - 2D string array grid | 
|  | 37 | +   */ | 
|  | 38 | +  findInitialPosition (grid: string[][]): void { | 
|  | 39 | +    const start = getGridCoordinate(grid, this.symbol) | 
|  | 40 | + | 
|  | 41 | +    this.pos.x = start.x | 
|  | 42 | +    this.pos.y = start.y | 
|  | 43 | +  } | 
|  | 44 | + | 
|  | 45 | +  /** Reads the next instruction and sets the (y,x) direction */ | 
|  | 46 | +  readInstruction (): void { | 
|  | 47 | +    this.instruction = this.instructions.pop() as GuardDirection || '-' | 
|  | 48 | + | 
|  | 49 | +    switch(this.instruction) { | 
|  | 50 | +    case GuardDirection.UP: | 
|  | 51 | +      this.direction = { x: 0, y: -1 } | 
|  | 52 | +      break | 
|  | 53 | +    case GuardDirection.DOWN: | 
|  | 54 | +      this.direction = { x: 0, y: 1 } | 
|  | 55 | +      break | 
|  | 56 | +    case GuardDirection.LEFT: | 
|  | 57 | +      this.direction = { x: -1, y: 0 } | 
|  | 58 | +      break | 
|  | 59 | +    case GuardDirection.RIGHT: | 
|  | 60 | +      this.direction = { x: 1, y: 0 } | 
|  | 61 | +      break | 
|  | 62 | +    default: | 
|  | 63 | +      break | 
|  | 64 | +    } | 
|  | 65 | +  } | 
|  | 66 | + | 
|  | 67 | +  /** Increments the robot's (y,x) coordinate by direction */ | 
|  | 68 | +  walk (): void { | 
|  | 69 | +    this.pos.x += this.direction.x | 
|  | 70 | +    this.pos.y += this.direction.y | 
|  | 71 | +  } | 
|  | 72 | + | 
|  | 73 | +  /** | 
|  | 74 | +   * Finds the next (y,x) coordinate of the robot or a given `Point` parameter. | 
|  | 75 | +   * @param {Point} [point] - (Optional) (y,x) coordinate to find the next step coorndinate from | 
|  | 76 | +   * @returns {Point} Next (y,x) coordinate after 1 step | 
|  | 77 | +   */ | 
|  | 78 | +  next (point?: Point): Point { | 
|  | 79 | +    const x = point?.x || this.pos.x | 
|  | 80 | +    const y = point?.y || this.pos.y | 
|  | 81 | + | 
|  | 82 | +    const nextX = x + this.direction.x | 
|  | 83 | +    const nextY = y + this.direction.y | 
|  | 84 | + | 
|  | 85 | +    return { | 
|  | 86 | +      x: nextX, y: nextY | 
|  | 87 | +    } | 
|  | 88 | +  } | 
|  | 89 | +} | 
0 commit comments