Skip to content

Instantly share code, notes, and snippets.

@q00u
Created December 1, 2021 20:50
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 q00u/899d5d20e415506c2b2f923e04ead2ae to your computer and use it in GitHub Desktop.
Save q00u/899d5d20e415506c2b2f923e04ead2ae to your computer and use it in GitHub Desktop.
const input = `103
106
107
110
121
132
147
148
...
9548`;
const test = `199
200
208
210
200
207
240
269
260
263`;
// SPLIT LINES
// const lines = test.split('\n');
const lines = input.split('\n');
// SPLIT CHARS
// const chars = test.split('');
// const chars = input.split('');
// PARSE CHARS
// const parsed = chars.map(char => parseInt(char));
// PARSE LINES
const parsed = lines.map(line => parseInt(line));
// Part 1:
function puzzle1() {
let count = 0;
for (let i = 1; i < parsed.length; i++) {
if (parsed[i] > parsed[i - 1]) {
count++;
}
}
return count;
}
const part1 = puzzle1();
console.log('Part 1:', part1);
// Part 2:
function puzzle2() {
let count = 0;
let sum = parsed[0] + parsed[1] + parsed[2];
for (let i = 3; i < parsed.length; i++) {
let newsum = parsed[i-2] + parsed[i-1] + parsed[i];
if (newsum > sum) {
count++;
}
sum = newsum;
}
return count;
}
const part2 = puzzle2();
console.log('Part 2:', part2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment