-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (79 loc) · 2.57 KB
/
main.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
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
function calculateDays(marbles) {
let vCount = 0, hrCount = 0, cycles = 0;
let mins = [];
let vmins = [];
let hrs = [];
let indicies = [];
let twelfball = 0;
let cycled = false;
let results = "";
let cycledQueue = populateArray();
let refQueue = populateArray();
let tempQueue = populateArray();
while (!cycled) {
//handles shifting 5 balls into the minute queue and tipping into the Vmin queue.
if (vCount < 12) {
shiftElements(mins, cycledQueue);
//mins = arr.splice(0, 5);
vmins.unshift(mins.shift());
dumpMarbles(cycledQueue, mins, 4);
vCount++;
}
//handles shifting balls into the hours queue and dumping the Vmin queue.
else {
hrs.unshift(vmins.shift());
dumpMarbles(cycledQueue, vmins, 11);
hrCount++;
vCount = 0;
}
//dumps the hours queue and breaks the while loop.
if (hrCount == 12) {
twelfball = hrs.shift();
dumpMarbles(cycledQueue, hrs, 11);
cycledQueue.push(twelfball);
hrCount = 0;
cycles = 1;
cycled = true;
}
}
//establish the pattern of movement across one cycle of the machine
for (var i = 0; i < marbles; i++) {
for (var j = 0; j < marbles; j++) {
if (refQueue[i] == cycledQueue[j]) {
indicies[i] = j;
}
}
}
//simulates 12 hour movement using the pattern map
while (cycledQueue.toString() !== refQueue.toString()) {
for (var i = 0; i < marbles; i++) {
tempQueue[indicies[i]] = cycledQueue[i];
}
cycledQueue = tempQueue.slice(0);
cycles++;
}
//////////////////////////
//returns the answer
return cycles / 2;
/////////////////////////
//fills the min queue with elements from the base queue
function shiftElements(arrGaining, arrLosing) {
for (var i = 0; i < 5; i++) {
arrGaining.unshift(arrLosing.shift());
}
}
//add back in reverse order from time queue to base queue
function dumpMarbles(arrGaining, arrLosing, number) {
for (var i = 0; i < number; i++) {
arrGaining.push(arrLosing.shift());
}
}
//util function
function populateArray() {
let arr = [];
for (var i = 0; i < marbles; i++) {
arr[i] = i;
}
return arr;
}
}