day 4
This commit is contained in:
1000
2022/4.input
Normal file
1000
2022/4.input
Normal file
File diff suppressed because it is too large
Load Diff
27
2022/4.js
Normal file
27
2022/4.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const rangeCheck = (a, b) => (a[0] <= b[0] && a[1] >= b[1])
|
||||||
|
||(b[0] <= a[0] && b[1] >= a[1]);
|
||||||
|
|
||||||
|
const pairs = fs.readFileSync('4.input')
|
||||||
|
.toString()
|
||||||
|
.split('\n')
|
||||||
|
.map(i => i.split(',')
|
||||||
|
.map(j => j.split('-')
|
||||||
|
.map(k => parseInt(k))));
|
||||||
|
|
||||||
|
const overlapingPairs = pairs.reduce((p, c) => {
|
||||||
|
if(rangeCheck(c[0], c[1])) p++;
|
||||||
|
return p;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
console.log(`Part 1: ${overlapingPairs}`);
|
||||||
|
|
||||||
|
const overlapCheck = (a, b) => a[0] <= b[1] && b[0] <= a[1];
|
||||||
|
|
||||||
|
const anyOverlapingPairs = pairs.reduce((p, c) => {
|
||||||
|
if(overlapCheck(c[0], c[1])) p++;
|
||||||
|
return p;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
console.log(`Part 2: ${anyOverlapingPairs}`);
|
||||||
Reference in New Issue
Block a user