<commit_before>var fs = require('fs');
var path = require('path');

solveMenuProblem();

function solveMenuProblem() {
  var problemInfo = getProblemFromFile(path.join(__dirname, 'problems.txt'));
  
  
}

function getNextProblemItems(problemArray, initialIndex, jumps) {
  var result = [];
  
  for (var i = initialIndex; i < (initialIndex + jumps) && i < problemArray.length; i++) {
    var splitted = problemArray[i].split(' ');
    result.push({ cost: +splitted[0], value: +splitted[1] });
  }
  
  return result;
}

function getProblemFromFile(fileName) {
  if (fs.existsSync(fileName)) {
    var data = fs.readFileSync(fileName, 'utf8');
    data = data.replace(/\r/g, '');
    var problemArray = data.split('\n');
    var problemInfo = [];
    
    for (var i = 0; i < problemArray.length; i++) {
      var splitted = problemArray[i].split(' ');
      
      if (splitted.length === 3) {
        var days = +splitted[0];
        var platesNumber = +splitted[1];
        var budget = +splitted[2];
        
        if (days !== 0 && platesNumber !== 0 && budget !== 0) {
          var plates = getNextProblemItems(problemArray, i + 1, platesNumber);
          problemInfo.push({
            days,
            platesNumber,
            budget,
            plates
          });
          
          i = i + platesNumber;
          if (i === problemArray.length - 1) {
            break;
          }
        }
      } else if (splitted.length === 2) {
        
      } else {
        
      }
    }
    
    return problemInfo;
  } else {
    console.log("The problems.txt file does not exist.");
  }
}

module.exports = solveMenuProblem;<commit_msg>File read logic is now made line by line.<commit_after>var fs = require('fs');
var path = require('path');
var readLine = require('readline');

solveMenuProblem();

function solveMenuProblem() {
  getProblemFromFileAsync(path.join(__dirname, 'problems.txt'))
    .then(problemInfo => {
      console.log(JSON.stringify(problemInfo, null, 4));
    })
    .catch(e => {
      console.log(e);
    });
}

function getProblemFromFileAsync(fileName) {
  return new Promise((resolve, reject) => {
    if (!fs.existsSync(fileName)) {
      return reject(`The file ${fileName} was not found.`);
    }
    var problemInfo = [];
    var lineReader = readLine.createInterface({
      input: fs.createReadStream(fileName)
    });
    
    var platesNumber = 0;
    var readPlates = 0;
      
    lineReader.on('line', (line) => {
      var splitted = line.split(' ');
      
      if (splitted.length === 3) {
        readPlates = 0;
        var days = +splitted[0];
        platesNumber = +splitted[1];
        var budget = +splitted[2];
        
        if (isNaN(days) || isNaN(platesNumber) || isNaN(budget)) {
          return reject(`Unrecognized line format (${line}).`);
        }
        
        if (days === 0 && platesNumber === 0 && budget === 0) {
          lineReader.close();
        } else {
          problemInfo.push({
            days,
            platesNumber,
            budget,
            plates: []
          });
        }
          
      } else if (splitted.length === 2) {
        readPlates++;
        var lastProblem = problemInfo[problemInfo.length -1];
        var plateCost = +splitted[0];
        var plateValue = +splitted[1];
        
        if (isNaN(plateCost) || isNaN(plateValue)) {
          return reject(`Unrecognized line format (${line}).`);
        }
        if (readPlates > platesNumber) { 
          return reject(`There were more plates than the specified in the previous line (${lastProblem.days} ${lastProblem.platesNumber} ${lastProblem.budget}).`);
        }
        
        var plate = { cost: +splitted[0], value: +splitted[1] };
        lastProblem.plates.push(plate);
      } else {
        return reject(`Unrecognized line format (${line}).`);
      }
    });

    lineReader.on('close', () => {
      resolve(problemInfo);
    });
  });
}

module.exports = solveMenuProblem;
{'fraction_non_alphanumeric': 0.14684435504148857, 'fraction_numerical': 0.0077948202162434, 'mean_word_length': 2.1849479583666933, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, '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': '6228340', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1188, 'n_words': 272}