-
Notifications
You must be signed in to change notification settings - Fork 0
/
6b.js
47 lines (38 loc) · 1.09 KB
/
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
42
43
44
45
46
47
let calc = (bankStr) => {
let bank = bankStr
.split(/\s+/)
.filter(i => parseInt(i).toString() === i)
.map(i => parseInt(i));
let findNextCandidateIndex = (bank) => {
let max = Math.max.apply(null, bank);
for (let i = 0; i < bank.length; i++)
if (bank[i] === max)
return i;
}
let prevStates = {};
let n = 0;
while (true) {
let key = bank.join(',');
if (prevStates[key])
break;
prevStates[key] = true;
let index = findNextCandidateIndex(bank);
let blocks = bank[index];
bank[index] = 0;
while (blocks > 0) {
index++;
if (index >= bank.length)
index = 0;
bank[index]++;
blocks--;
}
n++;
}
return { n, bank };
}
let r;
if ((r = calc(`0 2 7 0`)).n !== 5)
throw new Error(`0 2 7 0 = ${JSON.stringify(r)}, should = 5`);
let bankStr = `5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6`;
let first = calc(bankStr);
console.log(calc(first.bank.join(' ')).n)