-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentAgentRand.java
More file actions
165 lines (140 loc) · 5.5 KB
/
StudentAgentRand.java
File metadata and controls
165 lines (140 loc) · 5.5 KB
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
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
public class StudentAgentRand {
private static final int[] first3 = new int[3];
public static final AtomicLong seed = new AtomicLong(-1);
private static final long initTime = System.nanoTime();
private static final int[][] hints = new int[9][3];
private static DeadHunt dh;
public static TreasureHunt.TrialResult run(TreasureHunt game) {
// Your agent's logic here, using game.jumpTo, move..., search, etc.
// Must call game.submit() at the end.
// DO NOT create a TrialResult manually — they'll get a compiler error.
// Example: Dumb scan
int edgeLength = game.edgeLength;
if (seed.get() != -1) {
dh = new DeadHunt();
DeadHunt.Coordinate goal = dh.getGoalCoord();
game.jumpTo(goal.x(), goal.y(), goal.z());
System.out.println("Guessing goal: " + goal);
if (game.search() != null && !game.search().isHint())
return game.submit();
}
System.out.println("Running dumb hunt");
int nhints = 0;
boolean search = true;
for (int z = 0; z < edgeLength && search; z++) {
for (int y = 0; y < edgeLength && search; y++) {
for (int x = 0; x < edgeLength && search; x++) {
game.jumpTo(x, y, z);
if (game.search() != null && !game.search().isHint()) {
TreasureHunt.Coordinate goal = game.position();
first3[0] = goal.x();
first3[1] = goal.y();
first3[2] = goal.z();
System.out.println("Found Goal");
} else if (game.search() != null && game.search().isHint()) {
hints[nhints][0] = x;
hints[nhints][1] = y;
hints[nhints][2] = z;
nhints++;
}
search = nhints < 9;
}
}
}
System.out.println("First 3 ints: " + Arrays.toString(first3));
System.out.println("Found hints: " + nhints);
for (int[] hint : hints) {
System.out.print(Arrays.toString(hint) + " ");
}
System.out.println();
if (seed.get() == -1) {
long pow = 0;
int mag = ("" + initTime).length();
while (seed.get() == -1 && pow <= mag) {
pow += 3;
System.out.print("Pow: " + pow + " \r");
checkN(
initTime,
(long) Math.pow(10, pow),
game.edgeLength
);
}
System.out.println("Seed: " + seed.get() + " \r");
dh = new DeadHunt();
DeadHunt.Coordinate goal = dh.getGoalCoord();
System.out.println("Goal: " + goal);
}
game.jumpTo(first3[0], first3[1], first3[2]);
return game.submit(); // fallback (goal always exists)
}
/**
* Checks the next `next` seeds before `initial`
*
* @param initial Starting seed
* @param next Lower bound of initial
* @param bound Bound of generated ints
*/
private static void checkN(long initial, long next, int bound) {
long test = initial;
long bottom = Math.max(
initial - next,
0
);
while (
test > bottom &&
!fastCheckSeed(test, bound) &&
seed.get() == -1
)
test--;
// set the seed
if (fastCheckSeed(test, bound))
seed.set(test);
}
/**
* Checks if a seed produces the numbers in `first3`
*
* @param seed The nanoTime seed
* @param bound upper bound of numbers
* @return Whether the seed is valid
*/
private static boolean fastCheckSeed(long seed, int bound) {
// Causes long overflow
// seed ^ unique ^ multiplier & mask
long unique = (
(seed ^ 3447679086515839964L) ^ 0x5DEECE66DL)
& ((1L << 48) - 1);
// first int
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int first = ((int) (unique >>> (48 - 31))) % bound;
if (first != first3[0])
return false;
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int second = ((int) (unique >>> (48 - 31))) % bound;
if (second != first3[1])
return false;
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int third = ((int) (unique >>> (48 - 31))) % bound;
if (third != first3[2])
return false;
for (int i = 0; i < hints.length; i++) {
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int x = ((int) (unique >>> (48 - 31))) % bound;
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int y = ((int) (unique >>> (48 - 31))) % bound;
unique = (unique * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
int z = ((int) (unique >>> (48 - 31))) % bound;
boolean match = false;
for (int[] cord : hints) {
if (cord[0] == x && cord[1] == y && cord[2] == z) {
match = true;
break;
}
}
if (!match)
return false;
}
return true;
}
}