-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
149 lines (138 loc) · 4.62 KB
/
Main.java
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
package verm2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/*
* Author: (Github uName) thisTyler
*
* November 15 2018
*/
public class Main {
private static final int numItems = 100000;
private static final int startLevel = 0;
private static List<Integer> slotLevels;
private static final Random rand = new Random();
public static void main(String[] args) {
resetSlotLevels();
List<Integer> nums = new ArrayList<>();
for(int i = 0; i < numItems; i++) {
nums.add(howManyTo300());
resetSlotLevels();
}
System.out.println("Items Mean Value : " + mean(nums));
System.out.println("Items Median Value : " + median(nums));
nums = new ArrayList<>();
for(int i = 0; i < numItems; i++) {
nums.add(howManyBoxesTo300());
resetSlotLevels();
}
System.out.println("Boxes Mean Value : " + mean(nums));
System.out.println("Boxes Median Value : " + median(nums));
}
/**
* Resets the values of slotLevels
* to startLevel.
*/
private static void resetSlotLevels() {
slotLevels = new ArrayList<>();
for(int i = 0; i < 5; i++) slotLevels.add(startLevel);
}
/**
* Determines the number of items needed
* to reach 300 using randomized changes.
*
* @modifies slotLevels
* @param start is not null, the initial power level.
* @return the number of items needed to reach
* power level 300 across all 5 slots.
*/
private static int howManyTo300() {
int dP = (int)(rand.nextFloat() * 21.0) - 10;
int slot = (int)(rand.nextFloat() * 5.0);
if(dP > 0) {
slotLevels.set(slot, slotLevels.get(slot) + dP);
if(slotLevels.get(slot) > 300)
slotLevels.set(slot, 300);
}
double average = mean(slotLevels);
if(average >= 300.0)
return 1;
else
return 1 + howManyTo300();
}
/**
* Determines the number of boxes needed
* to reach 300 using randomized changes.
* Each box contains 3 items, which operate on the
* same initial power level across all slots.
* These items are assigned random item slots. (1-5)
* The maximum powered item in each slot gotten
* from the box overwrites the previous
* maximum power in that slot (assuming this
* item is more powerful than the previous
* maximum power level in that slot).
*
* @modifies slotLevels
* @param start is not null, the initial power level.
* @return the number of boxes needed to reach
* power level 300.
*/
private static int howManyBoxesTo300() {
Map<Integer, Integer> changeMap = new HashMap<>();
for(int i = 0; i < 3; i++) {
int slot = (int)(rand.nextFloat() * 5.0);
int dP = (int)(rand.nextFloat() * 21.0) - 10;
if(changeMap.containsKey(slot)) {
int d2 = changeMap.get(slot);
changeMap.put(slot, dP > d2 ? dP : d2);
} else {
changeMap.put(slot, dP);
}
}
for(Integer slot : changeMap.keySet()) {
int dP = changeMap.get(slot);
if(dP > 0) {
slotLevels.set(slot, slotLevels.get(slot) + dP);
if(slotLevels.get(slot) > 300)
slotLevels.set(slot, 300);
}
}
double average = mean(slotLevels);
if(average >= 300.0)
return 1;
else
return 1 + howManyBoxesTo300();
}
/**
* Computes the mean value of a list.
*
* @param list is not null, the list to be averaged.
* @return the average value of the integers in list.
*/
private static double mean(List<Integer> list) {
return Arrays.stream(list.toArray(new Integer[0]))
.reduce(0, (x,y) -> x + y) / (list.size()*1.0);
}
/**
* Computes the median value of a list.
*
* @param list is not null, the list to be averaged.
* @return the most commonly found integer in list.
*/
private static int median(List<Integer> list) {
Map<Integer, Integer> countMap = new HashMap<>();
int largest = 0;
for(Integer i : list) {
if(countMap.containsKey(i))
countMap.put(i, countMap.get(i) + 1);
else
countMap.put(i, 1);
if(countMap.get(i) > largest)
largest = i;
}
return largest;
}
}