-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11.js
51 lines (40 loc) · 1.07 KB
/
11.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
let getResult = (data) => {
let parse_ints = str => {
return str.split(' ').filter(val => val != '').map(val => parseInt(val.trim()));
}
let result = [];
let usedSet = new Set();
let is_candidate = false;
let n = parseInt(data[0]);
let arr = [0];
for (let i = 1; i <= n; i++) {
if (!arr[i]) {
let [count, ...tmp_arr] = parse_ints(data[i]);
arr[i] = count > 0 ? tmp_arr : [];
}
if (!usedSet.has(i)) {
let allow_counter = 0;
for (let item of arr[i]) {
if (!usedSet.has(item)) break;
allow_counter++;
}
if (allow_counter === arr[i].length) {
is_candidate = true;
}
}
if (is_candidate) {
result.push(i);
if (result.length === n) break;
usedSet.add(i);
is_candidate = false;
i = 0;
}
}
return result.reduce((acc, val, index) => acc += val * (index + 1));
}
const { count } = require('console');
const fs = require('fs');
let fileContent = fs.readFileSync("input.txt", "utf8");
const data = fileContent.toString().trim().split("\n");
const result = getResult(data);
fs.writeFileSync("output.txt", result.toString());