36 lines
710 B
JavaScript
Raw Normal View History

2024-12-02 10:50:22 +01:00
const fs = require("fs");
const file = "inputs/2";
const fileContent = fs.readFileSync(file, "utf-8").trim();
const reports = fileContent.split("\n");
let safeCount = 0;
for (const element of reports) {
const arr = element.trim().split(" ");
const numbers = arr.map(Number);
const isIncreasing = numbers.every(
(num, i) => i === 0 || num > numbers[i - 1],
);
const isDecreasing = numbers.every(
(num, i) => i === 0 || num < numbers[i - 1],
);
if (!(isIncreasing || isDecreasing)) {
continue;
}
const isValidJumps = numbers.every(
(num, i) => i === 0 || Math.abs(num - numbers[i - 1]) <= 3,
);
if (isValidJumps) {
safeCount += 1;
}
}
console.log(safeCount)