Last active
January 5, 2016 16:48
-
-
Save ghaiklor/df7520cee740072eaee7 to your computer and use it in GitHub Desktop.
Advent of Code (Day 24 Part 1)
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
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