How to Process the Multiline input in Node JS

Nishant KumarNishant Kumar
3 min read

Yet again as always while trying to solve the problem statement, logically I can easily do that but again I got lost on how to do the input processing. No idea let's look at it.

For example, suppose the Elves finish writing their items' Calories and end up with the following list:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

Data is just like that for each elves.

  • The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.

  • The second Elf is carrying one food item with 4000 Calories.

  • The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.

  • The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.

  • The fifth Elf is carrying one food item with 10000 Calories.

So I have to take out that fourth elf which has the 24000 cal and print it. I don't know how to process the file.

Let's look at the basics,

const fs = require('fs');

fs.readFile('./input.txt', 'utf8', (err, data) => {
  if (err) {
    throw err;
  }

  console.log(data);
});

So this code does the basic of taking all the data from the file and printing it on the console. This code is not really doing much, using the fs module of Node.js and its API to do file processing. If file is not found then it throws the error. Simple nothing much. Now I have to do something to group the data before the blank line.

Here is something like this for that thing.

let data = `1000
2000
3000

4000

5000
6000

7000
8000
9000

10000`;

let calories_list = data.split('\n\n').map(lines => {
  return lines.split('\n').reduce((sum, line) => {
    return sum + Number(line);
  }, 0);
});

console.log(calories_list);

In this code, the split method is used to divide the data string into an array of lines, with each element of the array representing a section of the data separated by two newline characters. The map method is then used to iterate over this array of lines, and for each element (which represents a section of the data separated by blank lines), the split method is used again to split the element into an array of individual lines.

The reduce method is then called on this array of lines, with the sum parameter representing the sum of the numbers on the lines, and the line parameter representing the current line being processed. For each line, the reduce method adds the number on the line to the sum variable, and returns the updated sum value. This process is repeated for each line in the array, and the final sum value is returned as the result of the map callback.

The resulting array of sums is then assigned to the calories_list variable, and the code logs this array to the console. The output is [6000, 4000, 11000, 24000, 10000], which are the sums of the numbers in each section of the data separated by blank lines.

Then all that is left is do these and it will provide the output as.

const sol1 = Math.max(...calories_list);
console.log(sol1);
0
Subscribe to my newsletter

Read articles from Nishant Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nishant Kumar
Nishant Kumar

Here to give you guys a good read, in a different way with each blog. Not a niche-specific blogger. To be honest, I'm just blogging with the aim of applying knowledge to achieve practical goals.