Last active
March 4, 2018 16:21
-
-
Save io-developer/ce65f9f71b7ce83844e3b9d234539f15 to your computer and use it in GitHub Desktop.
AdventOfCode 2017 day 2-2
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
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