Skip to content

Instantly share code, notes, and snippets.

@io-developer
Last active March 4, 2018 16:21
Show Gist options
  • Save io-developer/ce65f9f71b7ce83844e3b9d234539f15 to your computer and use it in GitHub Desktop.
Save io-developer/ce65f9f71b7ce83844e3b9d234539f15 to your computer and use it in GitHub Desktop.
AdventOfCode 2017 day 2-2
function calc(input) {
var checksum = 0;
input.split('\n').forEach(row => {
var nums = row.split('\t').map(n => parseInt(n));
checksum += findIntDivs(nums).reduce((a, b) => a + b, 0);
});
return checksum;
}
function findIntDivs(nums) {
var divs = [];
for (var i = 0; i < nums.length - 1; i++) {
var a = nums[i];
for (var j = i + 1; j < nums.length; j++) {
var b = nums[j];
var min = Math.min(a, b);
var max = Math.max(a, b);
if (max % min == 0) {
divs.push(max / min);
}
}
}
return divs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment