Skip to content

Instantly share code, notes, and snippets.

@edulan
Last active February 19, 2020 20:20
Show Gist options
  • Save edulan/2364453bd1a2f12a0bc634c3917c004c to your computer and use it in GitHub Desktop.
Save edulan/2364453bd1a2f12a0bc634c3917c004c to your computer and use it in GitHub Desktop.
#!/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)));
});
@edulan
Copy link
Author

edulan commented Feb 19, 2020

cat input.txt | ./solve.js > output.txt

@edulan
Copy link
Author

edulan commented Feb 19, 2020

⚠️ solve.js must be executable:

chmod u+x ./solve.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment