Skip to content

Instantly share code, notes, and snippets.

@supernovaplus
Last active December 2, 2019 22:23
Show Gist options
  • Save supernovaplus/7e6b8b4a4329d9aa4200e84b11efa8bf to your computer and use it in GitHub Desktop.
Save supernovaplus/7e6b8b4a4329d9aa4200e84b11efa8bf to your computer and use it in GitHub Desktop.
Day 1 - Advent of Code 2019
var input = `149579
95962
97899
etc...`;
input = input.split("\n").map(n=>Number(n));
const part1 = (number) => {
let val = (Math.floor(number/3)-2)
return val > 0 ? val : 0
};
console.log("Part 1: "+input.reduce((val1,val2)=>val1+part1(val2),0))
const part2 = (number) => {
let total = 0;
let lastnum = number;
while(lastnum !== 0){
let sum = part1(lastnum);
lastnum = sum;
total += sum;
}
return total;
}
console.log("Part 2: "+input.reduce((val1,val2)=>val1+part2(val2),0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment