day_04/guardMostFrequestAsleepCalculation.js
/**
 * Function parses all the sleep logs into structures.
 *
 * Structure:
 * {
 *   rawLog: string,   // original string
 *   guardId: number,  // guard's ID, number after #
 *   sleepSchedule: [  // array of sleeping schedules, contains only minutes
 *     {
 *       start: number, // the minute guard started sleeping
 *       end: number    // the minute guard stoped sleeping
 *     }
 *   ]
 * }
 * 
 * @param {string[]} sleepLogStrings sorted list of sorted strings
 * @returns {object[]} parsed sleep log structures
 */
Function parses all the sleep logs into structures.

@param {string[]} sleepLogStrings sorted list of sorted strings
@returns {object[]} parsed sleep log structures
Function parses all the sleep logs into structures . @param { string [] } sleepLogStrings sorted list of sorted strings @returns { object [] } parsed sleep log structures
function parseLogStrings (sortedSleepLogStrings) {
  let parsedSleepLogMap = {}
  let currentGuard = null
  for (let x = 0; x < sortedSleepLogStrings.length; x++) {
    const log = sortedSleepLogStrings[x]
    const isGuardChange = log.includes('Guard #')
    const isFallAsleep = log.includes('falls asleep')
    const isWakesUp = log.includes('wakes up')
    if (isGuardChange) {
      currentGuard = extractGuardIdFromLog(log)
      if (!parsedSleepLogMap[currentGuard]) {
        parsedSleepLogMap[currentGuard] = {
          rawLog: log,
          guardId: currentGuard,
          sleepSchedule: []
        }
      }
    } else if (isFallAsleep) {
      const sleepStart = extractMinutesFromLog(log)
      parsedSleepLogMap[currentGuard].sleepSchedule.push({
        start: sleepStart,
        startLog: log
      })
    } else if (isWakesUp) {
      const lastSleepSchedule = last(parsedSleepLogMap[currentGuard].sleepSchedule)
      const sleepEnd = extractMinutesFromLog(log)
      lastSleepSchedule.end = sleepEnd
      lastSleepSchedule.endLog = log
    }
  }
  const parsedSleepLogs = Object.values(parsedSleepLogMap)
  return parsedSleepLogs
}
function parseLogStrings ( sortedSleepLogStrings ) { let parsedSleepLogMap = { } let currentGuard = null for ( let x = 0 ; x < sortedSleepLogStrings . length ; x ++ ) { const log = sortedSleepLogStrings [ x ] const isGuardChange = log . includes ( 'Guard #' ) const isFallAsleep = log . includes ( 'falls asleep' ) const isWakesUp = log . includes ( 'wakes up' ) if ( isGuardChange ) { currentGuard = extractGuardIdFromLog ( log ) if ( ! parsedSleepLogMap [ currentGuard ] ) { parsedSleepLogMap [ currentGuard ] = { rawLog : log , guardId : currentGuard , sleepSchedule : [ ] } } } else if ( isFallAsleep ) { const sleepStart = extractMinutesFromLog ( log ) parsedSleepLogMap [ currentGuard ] . sleepSchedule . push ( { start : sleepStart , startLog : log } ) } else if ( isWakesUp ) { const lastSleepSchedule = last ( parsedSleepLogMap [ currentGuard ] . sleepSchedule ) const sleepEnd = extractMinutesFromLog ( log ) lastSleepSchedule . end = sleepEnd lastSleepSchedule . endLog = log } } const parsedSleepLogs = Object . values ( parsedSleepLogMap ) return parsedSleepLogs }
Function parses all the sleep logs into structures.
Function parses all the sleep logs into structures .
// a map will enable us quicker access