Skip to content

Instantly share code, notes, and snippets.

@pfgithub
Last active December 5, 2017 05:25
Show Gist options
  • Save pfgithub/9e0512bfdbb63a96bb92c635a6447135 to your computer and use it in GitHub Desktop.
Save pfgithub/9e0512bfdbb63a96bb92c635a6447135 to your computer and use it in GitHub Desktop.
Advent of Code Solutions 2017
// 1
function getcaptcha(digits){
let count = 0;
digits.split("").forEach((digit, i,ds) => {
let nextDigit = ds[i+1] || ds[0];
if(digit == nextDigit) count += parseInt(digit, 10);
});
return count;
}
// 2
function getrealarray(rawinput){
let resarray = [];
rawinput.split("\n").forEach(line => {
let resarrayline = [];
line.split(" ").forEach(number => {
resarrayline.push(parseInt(number, 10));
});
resarray.push(resarrayline);
});
return resarray;
}
function checksum(realarrayy){
let value = 0;
realarrayy.forEach((line) => {
let max = 0;
let min = Number.MAX_SAFE_INTEGER;
line.forEach(num => {
max = Math.max(num, max);
min = Math.min(num, min);
});
value += max - min;
});
return value;
}
// 3
function generateNumbersUntil(input){
let current = [1];
let size = [1];
while(current[current.length - 1] < input){
size[size.length] = size[size.length - 1] + 2;
current.push(Math.pow(size[size.length - 1], 2));
}
return [current, size];
}
function getRing(prevposL, sizeL){
let prevpos = prevposL[prevposL.length - 2];
let size = sizeL[sizeL.length - 1];
let top = [];
let bottom = [];
let left = [];
let right = [];
let i = 0;
for(i = 0; i < size - 2; i++){
right[i] = prevpos+1+i;
}
let j = 0;
for(j = i; j < i+size; j++){
top[j-i] = prevpos+1+j;
}
let k = 0;
for(k = j; k < j+size - 2; k++){
left[k-j] = prevpos+1+k;
}
let l = 0;
for(l = k; l < k+size; l++){
bottom[l-k] = prevpos+1+l;
}
return [right, top, left, bottom];
}
// do the rest manually with indexof and (.length-1)/2
// 4
function getValid(passphrases){
let numright = 0;
passphrases.split("\n").forEach(passphrase => {
let passphrases = passphrase.split(" ");
let double = {};
let success = true;
passphrases.forEach((phr) => {
if(double[phr]) success=false;
double[phr] = true;
});
if(success)numright++
});
return numright;
}
// forgot to add this one
function getValid(passphrases){
let numright = 0;
passphrases.split("\n").forEach(passphrase => {
let passphrases = passphrase.split(" ");
let double = {};
let success = true;
passphrases.forEach((phr) => {
phr = phr.split("").sort();
if(double[phr]) success=false;
double[phr] = true;
});
if(success)numright++
});
return numright;
}
// 5
function jumpstoarray(jumps){
let array = [];
jumps.split("\n").forEach(jump => array.push(parseInt(jump,10)));
return array
}
function dojumps(jumps){
let done = true;
let i = 0;
let jumpsal = 1;
while(done){
let j = i;
i+=jumps[i];
if(i>=jumps.length || i <0) done = false;
jumps[j] = jumps[j]+1;
jumpsal++;
if(jumpsal > 1000000) throw new Error("fail");
}
return jumpsal;
}
// I did this one really slow because the jumpsal>1000000 was originally 10000 and that wasn't enough
// 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment