12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { loadInput, numericAscSort, strToInt } from "../common.js";
- const input = loadInput(2, parseInput);
- solve(input);
- function parseInput(inputData) {
- const reports = inputData.split(/\n/).map((report) => report.split(" ").map(strToInt));
- return reports;
- }
- function solve(parsedInput) {
- let safeReports = 0;
- for(const report of parsedInput) {
- let currDirection = 0;
- let lastDirection = 0;
-
- let safe = false;
-
- for(let i = 0; i < report.length - 1; i++) {
- currDirection = numericAscSort(report[i], report[i + 1]);
- const difference = Math.abs(report[i] - report[i + 1]);
-
-
- if(i != 0 && lastDirection != currDirection) {
- safe = false;
- break;
- }
-
- else if(difference > 3) {
- safe = false;
- break;
- }
- else {
- safe = true;
- }
- lastDirection = currDirection;
- }
- if(safe) { safeReports++; }
- }
-
- console.log("Safe Reports:", safeReports);
- }
|