-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.js
84 lines (69 loc) · 2.06 KB
/
day4.js
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
78
79
80
81
82
83
84
const fs = require('fs');
const day4a = () => {
let result = 0;
// const filename = './data/day4test.txt';
const filename = './data/day4.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' });
const lines = data.split('\n');
lines.forEach(line => {
const arr = line.split(',');
const elf1 = arr[0].split('-').map(v => +v);
const elf2 = arr[1].split('-').map(v => +v);
if (isEncapsulated(elf1, elf2)) {
result += 1;
}
// console.log(elf1, elf2, isEncapsulated(elf1, elf2));
});
console.log('4a answer = ', result);
}
// arr1 = [5,7], arr2 = [7,9]
const isEncapsulated = (arr1, arr2) => {
let isEncapsulated = false;
let min1, max1, min2, max2;
min1 = arr1[0];
max1 = arr1[arr1.length - 1];
min2 = arr2[0];
max2 = arr2[arr2.length - 1];
// case 1. [1,2,3,4] [2,3]
isEncapsulated ||= (min1 <= min2 && max1 >= max2);
// case 2. [2,3] [1,2,3,4]
isEncapsulated ||= (min1 >= min2 && max1 <= max2);
return isEncapsulated;
}
/*
....567.. 5-7
......789 7-9
*/
const isOverlapped = (arr1, arr2) => {
let isOverlapped = false;
let min1, max1, min2, max2;
min1 = arr1[0];
max1 = arr1[arr1.length - 1];
min2 = arr2[0];
max2 = arr2[arr2.length - 1];
// case 1. [5,6,7] [7,8,9]
isOverlapped ||= (min1 >= min2 && min1 <= max2) || (max1 >= min2 && max1 <= max2);
// case 2. [7,8,9] [5,6,7]
isOverlapped ||= (min2 >= min1 && min2 <= max1) || (max2 >= min1 && max2 <= max1);
return isOverlapped;
}
const day4b = () => {
let result = 0;
// const filename = './data/day4test.txt';
const filename = './data/day4.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' });
const lines = data.split('\n');
lines.forEach(line => {
const arr = line.split(',');
const elf1 = arr[0].split('-').map(v => +v);
const elf2 = arr[1].split('-').map(v => +v);
if (isOverlapped(elf1, elf2)) {
result += 1;
}
// console.log(elf1, elf2, isOverlapped(elf1, elf2));
});
console.log('4b answer = ', result);
}
module.exports = { day4a, day4b };