Created
April 22, 2020 13:58
-
-
Save stephengfriend/fcdc818676cfcd5d8985e271c19f7a7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| process.stdin.resume(); | |
| process.stdin.setEncoding("utf-8"); | |
| var stdin_input = ""; | |
| process.stdin.on("data", function (input) { | |
| stdin_input += input; // Reading input from STDIN | |
| }); | |
| process.stdin.on("end", function () { | |
| main(stdin_input); // Execute our entrypoint when we've finished reading the input | |
| }); | |
| /** | |
| * Out main entrypoint to execute. | |
| * | |
| * Built-in functions used: | |
| * * String.prototype.split: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | |
| * * Array.prototype.map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | |
| * * Array.prototype.filter: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | |
| * * parseInt: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt | |
| */ | |
| function main(input) { | |
| const lines = input.split('\n'); // Split our input based on new line. | |
| const rainfallTimes = parseStringOfNumbers(lines[1]); // Parse the second line of inputs to capture the rainfall times. | |
| const dryingTimes = parseStringOfNumbers(lines[2]) // Parse the third line of inputs to capture the drying time of each piece of clothing on the line. | |
| const largestGap = findLargestGap(rainfallTimes) | |
| process.stdout.write(dryingTimes.filter((dryingTime) => dryingTime <= largestGap).length) | |
| } | |
| /** | |
| * All of our input lines are space separated integers in base 10. This will parse them into an array of numbers. | |
| */ | |
| function parseStringOfNumbers(stringOfNumbers) { | |
| return stringOfNumbers.split(' ').map((val) => parseInt(val, 10)); | |
| } | |
| /** | |
| * Since we know we can grab ALL dry clothes on any given pass, we only need to know | |
| * the largest available gap in rainfall. | |
| */ | |
| function findLargestGap(rainTimes) { | |
| let currentTime = rainTimes[0] // We know that we are beginning at the first time index. | |
| let largestGap = 0; | |
| for (let i = 1; i < rainTimes.length; i++) { | |
| const nextRainTime = rainTimes[i]; | |
| const gap = nextRainTime - currentTime; | |
| if (gap > largestGap) { | |
| largestGap = gap; | |
| } | |
| currentTime = nextRainTime; // Prepare for our next iteration of the loop. | |
| } | |
| return largestGap; | |
| } |
line 31 process.stdout.write(dryingTimes.filter((dryingTime) => dryingTime <= largestGap).length) // print out drytime using the condition inside d bracket .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you know that or how does a developer determine which build in functions to use to handle a particular problem ?
i saw that String.prototype.split''''' Array.prototype.map:''''' Array.prototype.filter:''''''' parseInt: are used