Skip to content

Instantly share code, notes, and snippets.

@Friss
Created December 9, 2020 15:33
Show Gist options
  • Save Friss/4e7b42084062cd7addcf3a75873d1131 to your computer and use it in GitHub Desktop.
Save Friss/4e7b42084062cd7addcf3a75873d1131 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
(async () => {
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`);
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`);
const inputs = inputData
.toString()
.split('\n')
.filter((n) => n)
.map((n) => parseInt(n, 10));
let offset = 0;
let sumMax;
for (let index = 25; index < inputs.length; index++) {
const element = inputs[index];
let foundSum = false;
for (let j = offset; j < index; j++) {
const sum1 = inputs[j];
for (let k = j + 1; k < index; k++) {
const sum2 = inputs[k];
if (sum1 + sum2 === element) {
foundSum = true;
}
}
}
offset++;
if (!foundSum) {
console.log('Didnt find', element);
sumMax = element;
}
}
for (let index = 0; index < inputs.length; index++) {
const element = inputs[index];
let goal = sumMax - element;
const ans = [element];
for (let j = index + 1; j < inputs.length; j++) {
const nextElement = inputs[j];
goal -= nextElement;
ans.push(nextElement);
if (goal === 0) {
ans.sort((a, b) => a - b);
console.log('part2', ans[0] + ans[ans.length - 1]);
return;
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment