-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
77 lines (64 loc) · 1.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { createInterface } from 'readline';
import { createReadStream } from 'fs';
function splitComma(line: string): string[] {
return line.split(',');
}
function splitDash(str: string): string[] {
return str.split('-');
}
function convertToNum(numArray: string[]) {
return numArray.map(num => parseInt(num, 10));
}
function getRangeSet([min, max]: number[]) {
const set = new Set<number>();
for (let i = min; i <= max; i++) {
set.add(i);
}
return set;
}
function setHasEveryElementFromOtherSet(testSet: Set<number>, compareTo: Set<number>) {
let hasAll = true;
for (const num of testSet.values()) {
if (!compareTo.has(num)) {
hasAll = false;
break;
}
}
return hasAll;
}
function setHasSomeElementFromOtherSet(testSet: Set<number>, compareTo: Set<number>) {
let hasSome = false;
for (const num of testSet.values()) {
if (compareTo.has(num)) {
hasSome = true;
break;
}
}
return hasSome;
}
function main() {
const stream = createReadStream('input.txt', 'utf-8');
const readline = createInterface({ input: stream });
let fullyOverlapCounter = 0;
let partiallyOverlapCounter = 0;
readline.on('line', function processLine(line: string) {
const [elf1, elf2] = splitComma(line)
.map(splitDash)
.map(convertToNum)
.map(getRangeSet);
if (
setHasEveryElementFromOtherSet(elf1, elf2) ||
setHasEveryElementFromOtherSet(elf2, elf1)
) {
fullyOverlapCounter++;
}
if(setHasSomeElementFromOtherSet(elf1, elf2)) {
partiallyOverlapCounter++;
}
});
readline.on('close', function processResults() {
console.log('Number of pairs where one range fully overlaps the other: ', fullyOverlapCounter);
console.log('Number of pairs where ranges parially overlap: ', partiallyOverlapCounter);
});
}
main();