Skip to content

Instantly share code, notes, and snippets.

@smt
Created December 4, 2017 15:22
Show Gist options
  • Save smt/cbab0a07f8f9e53be37d4540b2c5f785 to your computer and use it in GitHub Desktop.
Save smt/cbab0a07f8f9e53be37d4540b2c5f785 to your computer and use it in GitHub Desktop.
AoC 2017 - Day 2
function prepare(data) {
return data
.split('\n')
.map(x => x
.split('\t')
.map(Number)
);
}
function findMinMax(nums) {
return [
Math.min.apply(Math, nums),
Math.max.apply(Math, nums)
];
}
function findQuotient(nums) {
const sortedNums = nums.slice().filter(Boolean).sort((a, b) => b - a);
function go(xs) {
for (let i = 1; i < xs.length; i += 1) {
const mod = xs[0] % xs[i];
if (mod === 0) {
return xs[0] / xs[i];
}
}
return go(xs.slice(1));
}
return go(sortedNums);
}
function checksum(data) {
return prepare(data)
.map(findMinMax)
.reduce((a, [min, max]) => a + (max - min), 0);
}
function checksum2(data) {
return prepare(data)
.map(findQuotient)
.reduce((a, quotient) => a + quotient, 0);
}
//console.log('part 1:', checksum(input));
//console.log('part 2:', checksum2(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment