From 6c0e9c3fb327fc1cbdf7f4c1787b0b170c4c263d Mon Sep 17 00:00:00 2001 From: x0x7 Date: Tue, 3 Dec 2024 16:58:40 -0500 Subject: [PATCH] Add 2024/day2/part1.js --- 2024/day2/part1.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2024/day2/part1.js diff --git a/2024/day2/part1.js b/2024/day2/part1.js new file mode 100644 index 0000000..599944e --- /dev/null +++ b/2024/day2/part1.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +var fs = require('fs'); +var data = fs.readFileSync(process.argv[2],'utf8').split('\n').filter(i=>i).map(i=>i.split(' ').map(i=>parseInt(i))); + +var safety_count = 0; + + +function isreportsafe(report) { + var first = report.slice(0,-1); + var diffs = first.map((elem,idx)=>report[idx+1]-elem); + if(diffs[0]<0) { //Is negative + diffs = diffs.map(i=>-i); //Negate it + } + return !diffs.some(i=>(i<1 || i>3)); +} + +var safety_count = data.reduce((acc,report)=>acc+(isreportsafe(report)?1:0),0); + +console.log('Result:',safety_count); \ No newline at end of file