Skip to content

Instantly share code, notes, and snippets.

@zsteva
Last active December 15, 2024 16:48
Show Gist options
  • Save zsteva/e70157ccd52e8d6d7f7dbefbf27e606b to your computer and use it in GitHub Desktop.
Save zsteva/e70157ccd52e8d6d7f7dbefbf27e606b to your computer and use it in GitHub Desktop.
advent of code 2024 day 2 // javascript console
// part 1
function isSafe(row) {
if (row.isIncrease() || row.isDecrease()) {
return row.diff2().abs().filter((a) => (a > 3)).length == 0;
} else {
return false;
}
}
getInput().splitNL().splitCOL().nums().map((row) => isSafe(row)).filter((a) => a).length;
// result 606
// part 2
function isSafeRemove1(row) {
for (var i = 0; i < row.length; i++) {
if (isSafe(row.removeN(i))) {
return true;
}
}
return false;
}
getInput().splitNL().splitCOL().nums().map((row) => isSafeRemove1(row)).filter((a) => a).length;
// result 644
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment