<commit_before>/**
 * @name Instructions Parser
 * @desc Parse input and create an Instructions instance.
 * @param {String} string input
 * @returns {Object}
 */
module.exports = function instructionParser(input) {
  if (!input || typeof input !== "string") {
    throw new Error("Could not parse input.");
  }

  var firstNewLineIndex = input.indexOf("\n");
  var firstLine = input.slice(0, firstNewLineIndex).split(" ");
  var rest = input.slice(firstNewLineIndex);

  var robotInstructions = rest.split("\n\n");

  var areaSize = {
    xAxis: Number(firstLine[0]),
    yAxis: Number(firstLine[1])
  };

  if (areaSize.xAxis < 1 || areaSize.yAxis < 1) {
    throw new Error("Area size is invalid.");
  }

  var robots = robotInstructions.map(function(val, index) {
    var values = val.split("\n");
    // [0] is just a newline
    var startPosition = values[1].split(" ");
    var x = Number(startPosition[0]);
    var y = Number(startPosition[1]);
    var facing = startPosition[2];
    var instructions = values[2].split("");

    if (isNaN(x) || isNaN(y)) {
      throw new Error("Robot " + index + " start position is invalid.");
    }

    if (facing.length > 0 && !/[NSWE]/.test(facing)) {
      throw new Error("Robot " + index + " facing direction is invalid.");
    }

    if (instructions.length > 0 && !/[NSWEF]/.test(instructions)) {
      throw new Error("Robot " + index + " instructions are invalid.");
    }

    return {
      id: index,
      start: { x: x, y: y, facing: facing },
      instructions: instructions
    };
  });

  return {
    areaSize: areaSize,
    robots: robots
  };
};<commit_msg>Fix bug with robot instruction not being parsed properly<commit_after>/**
 * @name Instructions Parser
 * @desc Parse input and create an Instructions instance.
 * @param {String} string input
 * @returns {Object}
 */
module.exports = function instructionParser(input) {
  validateInput(input);

  var firstNewLineIndex = input.indexOf("\n");
  var firstLine = input.slice(0, firstNewLineIndex).split(" ");
  var rest = input.slice(firstNewLineIndex);

  var robotInstructions = rest.split("\n\n");

  var areaSize = {
    xAxis: Number(firstLine[0]),
    yAxis: Number(firstLine[1])
  };

  var robots = robotInstructions.map(function(val, index) {
    var values = val.trim().split("\n");
    var startPosition = values[0].split(" ");
    var x = Number(startPosition[0]);
    var y = Number(startPosition[1]);
    var facing = startPosition[2];
    var instructions = values[1].split("");

    return {
      id: index,
      start: { x: x, y: y, facing: facing },
      instructions: instructions
    };
  });

  return {
    areaSize: validateAreaSize(areaSize),
    robots: validateRobots(robots)
  };
};

function validateInput(input) {
  if (!input || typeof input !== "string") {
    throw new Error("Could not parse input.");
  }

  return input;
}

function validateAreaSize(areaSize) {
  if (areaSize.xAxis < 1 || areaSize.yAxis < 1) {
    throw new Error("Area size is invalid.");
  }

  return areaSize;
}

function validateRobots(robots) {
  robots.forEach(function(robot, index) {
    var x = robot.start.x, y = robot.start.y;
    var facing = robot.start.facing, instructions = robot.instructions;

    if (isNaN(x) || isNaN(y)) {
      throw new Error("Robot " + index + " start position is invalid.");
    }

    if (facing.length > 0 && !/[NSWE]/.test(facing)) {
      throw new Error("Robot " + index + " facing direction is invalid.");
    }

    if (instructions.length > 0 && !/[NSWEF]/.test(instructions)) {
      throw new Error("Robot " + index + " instructions are invalid.");
    }
  });

  return robots;
}
{'fraction_non_alphanumeric': 0.15382513661202185, 'fraction_numerical': 0.006830601092896175, 'mean_word_length': 3.208045977011494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2213013', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1130, 'n_words': 308}