-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6b.js
41 lines (35 loc) · 966 Bytes
/
6b.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
/*jshint esversion: 6 */
let banks = "0 5 10 0 11 14 13 4 11 8 8 7 1 4 12 11";
let _ = require("lodash");
let history = [];
function balance(orig) {
let array = orig.slice();
let index = _.indexOf(array, _.max(array));
let val = array[index];
array[index] = 0;
for (; val > 0; val--) {
index++;
if (index >= array.length) {
index = 0;
}
array[index] += 1;
}
// console.log(`balanced: sum = ${_.sum(array)}`)
return array;
}
let current = banks.split("\t").map(v => parseInt(v));
while (true) {
let balanced = balance(current);
let sig = balanced.join(',');
if (history.find(x => x === sig) !== undefined) {
break;
}
history.push(sig);
current = balanced;
}
let balanced = balance(current);
let sig = balanced.join(',');
let index = history.findIndex(v => v === sig);
let cycle = history.length - index;
console.log(`cycle size is ${cycle}`);
// 1695