adventofcode2022/day4/solution.ts
2022-12-04 00:12:59 -05:00

33 lines
811 B
TypeScript

import { readFileSync } from 'fs';
import { join } from 'path';
const range = (start, end) => Array.from(Array(end - start + 1).keys()).map(x => x + start);
const fileString = readFileSync(join(__dirname, "input"), 'utf-8');
const data = fileString.split("\n");
var overlaping: number = 0;
var anyOverlaping: number = 0;
for (const pair of data) {
const elves = pair.split(",");
if (elves.length != 2) {
continue;
}
const e1 = elves[0].split("-");
const e2 = elves[1].split("-");
const e1r = range(+e1[0], +e1[1]);
const e2r = range(+e2[0], +e2[1]);
if (e1r.every(val => e2r.includes(val)) || e2r.every(val => e1r.includes(val))) {
overlaping += 1;
}
if (e1r.some(val => e2r.includes(val))) {
anyOverlaping += 1;
}
}
console.log(overlaping);
console.log(anyOverlaping);