Skip to content

Instantly share code, notes, and snippets.

@spacecowb0y
Last active Dec 2, 2021
Embed
What would you like to do?
Advent of Code 2021: Javascript
const measurements = []; // your data here
const byIndexValue = (value, index, array) => array[index] > array[index - 1];
const byIndexSum = (value, index, array) =>
value + array[index + 1] + array[index + 2] >
value + array[index - 1] + array[index + 1];
console.log(`Answer 1: ${measurements.filter(byIndexValue).length}`);
console.log(`Answer 2: ${measurements.filter(byIndexSum).length}`);
const instructions = []; // your data here
let [horizontal, depth, aim] = Array(3).fill(0)
const dive = (value, index, array) => {
let intruction = value.split(" "),
command = intruction[0],
val = parseInt(intruction[1]);
if (command == "forward") (horizontal += val), (depth += aim * val);
if (command == "down") aim += val;
if (command == "up") aim -= val;
};
instructions.filter(dive);
console.log(`Answer 2: ${horizontal * depth}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment