Last active
February 19, 2020 20:20
-
-
Save edulan/2364453bd1a2f12a0bc634c3917c004c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env node | |
require("lodash.combinations"); | |
const { flatMap, combinations } = require("lodash"); | |
const readline = require("readline"); | |
const lines = []; | |
function processInput(lines) { | |
const [firstLine, lastLine] = lines; | |
const [max] = firstLine.split(" "); | |
const pizzas = lastLine.split(" "); | |
return { | |
max: parseInt(max), | |
pizzas: pizzas.map(number => parseInt(number)) | |
}; | |
} | |
function transform({ max, pizzas }) { | |
return flatMap(pizzas, (v, i, a) => combinations(a, i + 1)) | |
.reduce((currentSeq, seq) => { | |
const partialTotal = seq.reduce((total, number) => total + number, 0); | |
if (partialTotal < max) { | |
return seq; | |
} | |
return currentSeq; | |
}) | |
.map(number => pizzas.indexOf(number)); | |
} | |
function processOutput(data) { | |
console.log(data.length); | |
console.log(data.join(" ")); | |
} | |
readline | |
.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
}) | |
.on("line", function(line) { | |
lines.push(line); | |
}) | |
.on("close", function() { | |
processOutput(transform(processInput(lines))); | |
}); |
chmod u+x ./solve.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cat input.txt | ./solve.js > output.txt