Restructure folders and add day 3's solution
This commit is contained in:
parent
ecab4f263a
commit
519c1b63cc
6 changed files with 123 additions and 1 deletions
36
javascript/day_02/part1.mjs
Normal file
36
javascript/day_02/part1.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { readFile } from "fs/promises";
|
||||
|
||||
function legalValues(a, b, gap) {
|
||||
const delta = Math.abs(a - b);
|
||||
console.log(`legaValues(${a}, ${b}, ${gap}){delta=${delta}} = ${delta <= gap}`);
|
||||
return delta <= gap;
|
||||
};
|
||||
|
||||
if (process.argv.length <= 2) {
|
||||
console.error(`Needs more args >:(`).
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const fileToRead = process.argv[2];
|
||||
const file = await readFile(fileToRead, `utf-8`);
|
||||
|
||||
let validLines = 0;
|
||||
for (const line of file.split(`\n`)) {
|
||||
// console.log(line)
|
||||
const ints = line.split(` `)
|
||||
.map(v => Number.parseInt(v));
|
||||
|
||||
const isSafe = ints
|
||||
.slice(0, -1)
|
||||
.map((v, i) => v - ints[i + 1])
|
||||
.every((v, _, a) => Math.sign(v) === Math.sign(a[0]) && Math.abs(v) <= 3);
|
||||
|
||||
// console.log(isSafe)
|
||||
|
||||
if(isSafe) {
|
||||
validLines++;
|
||||
};
|
||||
// break
|
||||
};
|
||||
|
||||
console.log(`Number of valid reports:`, validLines)
|
||||
Loading…
Add table
Add a link
Reference in a new issue