-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.js
60 lines (57 loc) · 1.14 KB
/
05.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
/**
* @param {string} row
* @param {number} searchRange
* @param {string} sep
*/
const binSearch = (data, searchRange, sep) => {
let search = [...Array(searchRange).keys()];
const items = data.split('');
items.forEach(item => {
const half = Math.ceil(search.length / 2);
switch (item) {
case sep[0]: {
search = search.splice(0, half);
break;
}
case sep[1]: {
search = search.splice(half);
break;
}
}
});
return search[0];
};
/**
* @param {string} d
*/
const part1 = async d => {
const seats = [];
d.split('\n').map(e => {
const row = binSearch(e.substring(0, 7), 128, 'FB');
const col = binSearch(e.substring(7), 8, 'LR');
seats.push(row * 8 + col);
});
return Math.max(...seats);
};
/**
* @param {string} d
*/
const part2 = async d => {
const seats = [];
d.split('\n').map(e => {
const row = binSearch(e.substring(0, 7), 128, 'FB');
const col = binSearch(e.substring(7), 8, 'LR');
seats.push(row * 8 + col);
});
seats.sort();
for (let i = 1; i < seats.length; i++) {
if ((seats[i] - seats[i - 1]) == 2) {
return seats[i] - 1;
}
}
return 0;
};
module.exports = {
part1,
part2
};