-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosingtheloop.js
More file actions
94 lines (67 loc) · 1.61 KB
/
closingtheloop.js
File metadata and controls
94 lines (67 loc) · 1.61 KB
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
85
86
87
88
89
90
91
92
93
94
function outString(num, length){
const str = `Case #${num}: ${length}`;
console.log(str);
}
function separateNumAndLastChar(str){
const chr = str[str.length -1];
const num = parseInt(str.slice(0,-1))
return [num, chr];
}
function doSomething(line = '5B'){
const dict = {}
const arr = line.split(' ');
arr.forEach( x => {
const arr2 = separateNumAndLastChar(x);
if(dict[arr2[1]]){
dict[arr2[1]].push(arr2[0])
}
else {
dict[arr2[1]] = [arr2[0]]
}
});
return dict;
}
function zip(arr1, arr2){
let i = 0, j = 0;
const smallDouble = Math.min(arr1.length, arr2.length) * 2;
const arr = new Array(smallDouble )
while(arr1[i] && arr2[i]){
arr[j] = arr1[i];
j++;
arr[j] = arr2[i];
j++;
i++;
}
return arr;
}
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let n, m, count = 0;
rl.on('line', (line) => {
if(!n){
n = parseInt(line);
}
else if(!m){
count++;
m = parseInt(line);
}
else {
const dict = doSomething(line);
const arrR = dict['R'];
const arrB = dict['B'];
if(!arrR || ! arrB){
outString(count, 0)
}
else {
arrR.sort( (a,b) => b-a);
arrB.sort( (a,b) => b-a);
const arr = zip(arrR, arrB)
const length = arr.reduce( (a,b) => a+b) - arr.length;
outString(count, length);
}
m = 0;
}
});