|  | 
|  | 1 | +import { arrayToObject, isEvenDigits, halfDigit } from './utils.js' | 
|  | 2 | + | 
|  | 3 | +/** | 
|  | 4 | + * @class Blinker | 
|  | 5 | + * @description A class that counts the number of transformed and new stones after blinking. | 
|  | 6 | + */ | 
|  | 7 | +export class Blinker { | 
|  | 8 | +  /** Stone numbers tracking storage at each blink */ | 
|  | 9 | +  numberStorage: Record<number, number> = {} | 
|  | 10 | + | 
|  | 11 | +  /** Total number of stones after the last blink */ | 
|  | 12 | +  stoneCount: number = 0 | 
|  | 13 | + | 
|  | 14 | +  /** | 
|  | 15 | +   * Stores a number in the `this.numberStorage` object as a key with value. | 
|  | 16 | +   * @param {number} stoneNum - Number to store in an Object data | 
|  | 17 | +   * @param {number} value - Number value associated with the `stoneNum` number key | 
|  | 18 | +   * @returns {void} | 
|  | 19 | +   */ | 
|  | 20 | +  store (stoneNum: number, value: number = 1): void { | 
|  | 21 | +    if (this.numberStorage[stoneNum] === undefined) { | 
|  | 22 | +      this.numberStorage[stoneNum] = value | 
|  | 23 | +    } else { | 
|  | 24 | +      this.numberStorage[stoneNum] += value | 
|  | 25 | +    } | 
|  | 26 | +  } | 
|  | 27 | + | 
|  | 28 | +  /** | 
|  | 29 | +   * Simulates the quiz's "blink" and stones morphing. | 
|  | 30 | +   * Calculates the number of transformed stones after blinking `blinkTimes` | 
|  | 31 | +   * @param {number[]} stones - Array of numbers (stones) | 
|  | 32 | +   * @param {number} blinkTimes - Number of times to simulate "blinking" | 
|  | 33 | +   * @returns {number} Total number of transformed stones after blinking | 
|  | 34 | +   */ | 
|  | 35 | +  blink (stones: number[], blinkTimes: number): number { | 
|  | 36 | +    let unique = arrayToObject([...stones]) | 
|  | 37 | + | 
|  | 38 | +    for (let i = 0; i < blinkTimes; i += 1) { | 
|  | 39 | +      for (const num in unique) { | 
|  | 40 | +        const numberKey = Number(num) | 
|  | 41 | +        const count = unique[numberKey] as number | 
|  | 42 | + | 
|  | 43 | +        if (numberKey === 0) { | 
|  | 44 | +          this.store(1, count) | 
|  | 45 | +        } else if (isEvenDigits(numberKey)) { | 
|  | 46 | +          const group = halfDigit(numberKey) | 
|  | 47 | + | 
|  | 48 | +          group.forEach(part => { | 
|  | 49 | +            this.store(part, count) | 
|  | 50 | +          }) | 
|  | 51 | +        } else { | 
|  | 52 | +          this.store(numberKey * 2024, count) | 
|  | 53 | +        } | 
|  | 54 | +      } | 
|  | 55 | + | 
|  | 56 | +      unique = { ...this.numberStorage } | 
|  | 57 | +      this.numberStorage = {} | 
|  | 58 | + | 
|  | 59 | +      this.stoneCount = Object | 
|  | 60 | +        .values(unique) | 
|  | 61 | +        .reduce( | 
|  | 62 | +          (sum, ct) => sum += ct, 0 | 
|  | 63 | +        ) | 
|  | 64 | +    } | 
|  | 65 | + | 
|  | 66 | +    return this.stoneCount | 
|  | 67 | +  } | 
|  | 68 | +} | 
0 commit comments