Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghaiklor/df7520cee740072eaee7 to your computer and use it in GitHub Desktop.
Save ghaiklor/df7520cee740072eaee7 to your computer and use it in GitHub Desktop.
Advent of Code (Day 24 Part 1)
const Combinatorics = require('./combinatorics');
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n').map(Number);
const TOTAL_SUM = INPUT.reduce((total, x) => total + x, 0);
const GROUP_WEIGHT = TOTAL_SUM / 3;
const VALID_PACKAGES = [];
for (var i = 1; VALID_PACKAGES.length === 0; i++) {
var combination = Combinatorics.combination(INPUT, ++i);
var cmb;
while (cmb = combination.next()) {
if (cmb.reduce((acc, x) => acc + x) === GROUP_WEIGHT) VALID_PACKAGES.push(cmb);
}
}
const result = VALID_PACKAGES.map(pkg => pkg.reduce((acc, x) => acc * x)).sort((a, b) => a - b)[0];
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment