Skip to content

Instantly share code, notes, and snippets.

@amwmedia
Last active December 6, 2017 14:55
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 amwmedia/43a54d965353d3bea8bdbdaa21314fce to your computer and use it in GitHub Desktop.
Save amwmedia/43a54d965353d3bea8bdbdaa21314fce to your computer and use it in GitHub Desktop.
Advent Of Code 2017
// Day 1 - Challenge #1
input.split('').reduce((acc, d, i, a) => {
const n1 = Number(d);
const n2 = Number(a[i+1] || a[0]);
return (n1 === n2 ? acc + n1 : acc);
}, 0);
// Day 1 - Challenge #2
input.split('').reduce((acc, d, i, a) => {
const n2Pos = (i + (a.length / 2)) % a.length;
const n1 = Number(d);
const n2 = Number(a[n2Pos]);
return (n1 === n2 ? acc + n1 : acc);
}, 0);
// Day 2 - Challenge #1
// input preformatted to => [[1,2,3],[4,5,6],etc]
input
.map(numList => Math.max.apply(null, numList) - Math.min.apply(null, numList))
.reduce((acc, num) => acc + num, 0);
// Day2 - Challenge #2
input.reduce((acc, numList) => {
return acc + numList.reduce((acc2, n, i, a) => {
const num2 = a.find(n2 => n !== n2 && n2 % n === 0);
if (num2 != null) { acc2 = num2 / n; }
return acc2;
}, 0);
}, 0);
// Day 3 - Challenge #1
function getDist(num) {
let sqrt = Math.ceil(Math.sqrt(num));
let sideLengthOfSq = (sqrt % 2 === 0 ? sqrt + 1 : sqrt);
let highestNumInSq = sideLengthOfSq * sideLengthOfSq;
let targetDistFromCorner = (highestNumInSq - num) % (sideLengthOfSq - 1);
let targetDistFromSideCenter = Math.abs(targetDistFromCorner - Math.floor(sideLengthOfSq/2));
const closestDistFromSq = (sideLengthOfSq - 1) / 2;
return closestDistFromSq + targetDistFromSideCenter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment