-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.js
227 lines (204 loc) · 7.42 KB
/
script.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
function optimize() {
var errorMessage = ""
var students = parseInt(document.getElementById("students").value)
var t = parseFloat(document.getElementById("t").value)
var ft = parseFloat(document.getElementById("ft").value)
if (ft <= 0)
errorMessage += "The value of f(t) must be greater than 0, student distribution cannot be calculated."
var stars = parseFloat(document.getElementById("stars").value)
var adBonus = document.getElementById("adbonus").checked
var acceleration = document.getElementById("acceleration").checked
var accelerationBonus = parseFloat(document.getElementById("accelmult").value)
if(isNaN(accelerationBonus))
accelerationBonus = 2.8538;
var log10dmu = ft
if (ft < 100) {
//More accurate calculations
var db = Math.pow(Math.pow(10,ft),0.8) / 4000000
var log10db = Math.log10(db+10)
var dpsi = Math.pow(2,(ft/25)-1) - 0.5
var log10dpsi = Math.log10(dpsi+10)
} else{
//Less accurate but works for higher ft
var log10db = (ft * 0.8) - Math.log10(4e6)
var log10dpsi = (ft / 25.0 - 1) * Math.log10(2)
}
var dtSpeedUpgrades = ft / (15.0 * Math.log10(2))
var dtSpeed = (dtSpeedUpgrades + 0.1) / 10
var dtLevels = ft / Math.log10(4)
var dt = dtSpeed * dtLevels
if (adBonus) { dt *= 1.5 }
if (acceleration) { dt *= accelerationBonus }
function getCost(num) {
if (num % 2 === 1) {
return (Math.pow(num, 2) - 1) / 4 + (num + 1) / 2
} else {
return (Math.pow(num, 2) + 2 * num) / 4
}
}
function getNextCost(num) {
return getCost(num+1) - getCost(num);
}
function getCostOrder(order) {
cost = 0;
for (var i = 0; i < order.length-1; i++) {
cost += getCost(order[i]);
}
cost += 2*order[6]
return cost
}
if (errorMessage === "") {
var boosts = [
Math.log10(dt),
0.7 * Math.log10(1 + t),
Math.log10(1 + stars),
log10db / Math.sqrt(100 * log10db),
log10dmu / 1300.0,
log10dpsi / 225 * Math.sqrt(log10dpsi),
0.1
]
}
function getTotalBoost(order) {
res = 0
for (var i = 0; i < order.length-1; i++) {
res += order[i]*boosts[i]
}
return (1+order[6]*0.1)*res
}
var order = [0, 0, 0, 0, 0, 0, 0]
function getBestTotal(order, totalStudents, highest = [0,0,0,0,0,0,0]) {
var remainingStudents = totalStudents - getCostOrder(order)
var bestBoostPerCost = 0
var bestI = -1
for (var i = 0; i < order.length; i++) {
if ((i >= 3 && i < 6 && order[i] === 8) || (i === 6 && order[i] === 6)) {
continue
}
var boostPerCost = 0;
var cost = 0;
if (i === 6) {
bestPerCost = 0.05 * (
order[0] * boosts[0] + order[1] * boosts[1] + order[2] * boosts[2] +
order[3] * boosts[3] + order[4] * boosts[4] + order[5] * boosts[5]
)
cost = 2
}
else {
cost = getNextCost(order[i])
extraBoost = (1 + order[6] * boosts[6]) * boosts[i]
bestPerCost = extraBoost / cost
}
if (bestPerCost > bestBoostPerCost) {
if (cost > remainingStudents) {
continue
}
bestBoostPerCost = bestPerCost
bestI = i
bestCost = cost
}
}
if (bestI !== -1) {
order[bestI] += 1
if (getTotalBoost(order) > getTotalBoost(highest)) {
highest = order.slice()
}
return [order, totalStudents, highest]
}
else {
return [order, 0, highest]
}
}
if (errorMessage === "") {
var tempStudents = students
var highest = order.slice()
while (tempStudents > 0) {
var res = getBestTotal(order, tempStudents, highest)
order = res[0]
tempStudents = res[1]
highest = res[2]
}
}
function contains(arrayOfArrays, searchArray) {
var res = false
for (var i = 0; i < arrayOfArrays.length; i++) {
var array1 = arrayOfArrays[i]
res = array1.length === searchArray.length && array1.every(function(value, index) { return value === searchArray[index]})
if (res) {
return true
}
}
return false
}
function getAllForks(order, students, highest, required = [0,0,0,0,0,0,0]){
var maxBuy = [-1, -1, -1, 8, 8, 8, 6]
var tmp = []
for (var i = 0; i < order.length; i++) {
tmp1 = order.slice()
tmp1[i] = order[i] +1
if (maxBuy[i] > 0) {
tmp1[i] = Math.min(maxBuy[i], tmp1[i])
}
if (getTotalBoost(tmp1) < getTotalBoost(highest)) {
continue
}
tmp2 = required.slice()
tmp2[i] = order[i]+1
if (maxBuy[i] > 0) {
tmp2[i] = Math.min(maxBuy[i], tmp2[i] )
}
if (getCostOrder(tmp2) > students) {
continue
}
if (!contains(tmp,tmp2)) {
tmp.push(tmp2)
}
}
return tmp
}
if (errorMessage === "") {
var possibles = getAllForks(highest, students, highest)
var done_possibles = []
while (possibles.length > 0) {
var possible = possibles.shift()
done_possibles.push(possible.slice())
order = possible.slice()
total_students = students
while (total_students > 0) {
var res = getBestTotal(order, total_students, highest)
order = res[0]
total_students = res[1]
highest = res[2]
}
tmp_possibles = getAllForks(order, students, highest, possible)
for (var tmp_possible_i = 0; tmp_possible_i < tmp_possibles.length; tmp_possible_i++) {
var tmp_possible = tmp_possibles[tmp_possible_i]
if (contains(possibles, tmp_possible) || contains(done_possibles,tmp_possible)) {
continue
}
else {
possibles.push(tmp_possible)
}
}
}
order = highest
var phi = (
(1 + order[6] * boosts[6]) *
(
order[0] * boosts[0] + order[1] * boosts[1] + order[2] * boosts[2] +
order[3] * boosts[3] + order[4] * boosts[4] + order[5] * boosts[5]
)
)
var phiDigits = Math.pow(10, phi - Math.floor(phi))
}
var bonusText = ""
document.getElementById("output").innerHTML = (
/*
"Students: " + students + "<br>t: " + t + "<br>f(t): ee" + ft +
"<br>Stars: " + stars + "<br>Ad-bonus: " + adBonus +
"<br>Acceleration: " + acceleration + " and " + accelerationBonus + "x" +
"<br>" +
*/
errorMessage !== "" ? errorMessage : "Calculated dt: " + dt.toPrecision(3) + "<br>Phi: " + phiDigits.toPrecision(3) + "e" + Math.floor(phi) +
"<br>Student distribution: " + order.join(", ") + "<br>Total Students Spent: " + getCostOrder(order) + bonusText
)
}