This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utools.java
494 lines (458 loc) · 11.8 KB
/
utools.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* USACOTOOLS-Official version
* This is the official version.
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.*;
import java.util.regex.*;
public abstract class utools {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public static int ERRORS=0;
public static ArrayList<Exception> console=new ArrayList<Exception>();
public static String error="Error";
public static int debugcode=-1;
public static boolean DEBUG=false;
public static boolean lock;
public static boolean IO=true;
public static void blockio() {
IO=false;
}
public static boolean isrect(int[][] map,int x,int y) {
int cachedsize=-1;
int cachey=-1;
for(int i=x;i<map.length;i++) {
if(x>cachedsize) {
return false;
}
for(int j=y;j<map.length;j++) {
if(map[x][y]==0) {
cachey=y;
cachedsize=x;
}
if(y>cachey) {
return false;
}
}
}
return true;
}
public static Set<String> sclones(Set<String> k) {
return (new HashSet<String>(k));
}
public static Set<Integer> sclone(Set<Integer> k) {
return (new HashSet<Integer>(k));
}
public static Set<Long> sclonel(Set<Long> k) {
return (new HashSet<Long>(k));
}
public static boolean smartequals(int[] a,int[] b) {
if(a.length!=b.length) {
return false;
}
for(int i=0;i<a.length;i++) {
if(a[i]!=b[i]) {
return false;
}
}
return true;
}
public static boolean smartequals2D(int[][] a,int[][] b) {
if(a.length!=b.length) {
return false;
}
for(int i=0;i<a.length;i++) {
if(smartequals(a[i],b[i])) {
return false;
}
}
return true;
}
public static void report(Exception e) {
console.add(e);
ERRORS++;
}
public static ArrayList<Integer> touching(int[][] map,int x,int y){
ArrayList<Integer> out=new ArrayList<Integer>();
try {
out.add(map[x-1][y]);
}catch(Exception e) {
}
try {
out.add(map[x+1][y]);
}catch(Exception e) {
}
try {
out.add(map[x][y-1]);
}catch(Exception e) {
}
try {
out.add(map[x][y+1]);
}catch(Exception e) {
}
return out;
}
public static String repeat(int count, String with) {
return new String(new char[count]).replace("\0", with);
}
public static String changen(int position, char ch, String str){
char[] charArray = str.toCharArray();
charArray[position] = ch;
return new String(charArray);
}
public static BufferedReader mreader(String filen) throws IOException {
//Make a reader
return new BufferedReader(new FileReader(filen));
}
public static PrintWriter mwriter(String filen) throws IOException {
return new PrintWriter(new BufferedWriter(new FileWriter(filen)));
}
public static Scanner getsysscan() {
return new Scanner(System.in);
}
public static int binarySearch(int arr[], int l, int r, int x)
{
/*
* Binary Search
*
*/
if (r>=l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
public static int[][] copy2D(int[][] a){
int[][] b=new int[a.length][];
for(int i=0;i<a.length;i++) {
b[i]=new int[a[i].length];
for(int j=0;j<a[i].length;j++) {
b[i][j]=a[i][j];
}
}
return b;
}
public static int[] copyarr(int[] a) {
int[] b=new int[a.length];
for(int i=0;i<a.length;i++) {
b[i]=a[i];
}
return b;
}
public static int ebs(int arr[], int l, int r, int x) {
Arrays.sort(arr);
return binarySearch(arr, l, r, x);
}
public static int lsearch(int[] a,int b) {
for(int i=0;i<a.length;i++) {
if(a[i]==b) {
return i;
}
}
return -1;
}
public static void print(String out) {
System.out.print(out+"\n");
}
public static void printf(String out) {
System.out.print(out+"\n");
}
public static void print(String out,String end) {
System.out.print(out+end);
}
public static void print(String out,boolean flush) {
System.out.print(out+"\n");
if(flush) {
System.out.flush();
}
}
public static int[] toArray(ArrayList<Integer> arr) {
int[] stuff=new int[arr.size()];
for(int i=0;i<arr.size();i++) {
stuff[i]=arr.get(i);
}
return stuff;
}
public static String[] toArrays(ArrayList<String> arr) {
String[] stuff=new String[arr.size()];
for(int i=0;i<arr.size();i++) {
stuff[i]=arr.get(i);
}
return stuff;
}
public static void exit() {
System.exit(1);
}
public static void exit(int code) {
System.exit(code);
}
public static long benchmark() {
return System.currentTimeMillis();
}
public static long benchmark2() {
return System.nanoTime();
}
public static void clear(){
//Clears Screen in java
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (Exception e) {report(e);}
}
public static void console() {
System.out.println("Total Errors: "+ERRORS);
for(Exception a:console) {
if(ESTACK) {
print(a.getStackTrace().toString());
}
if(EMSG){print(a.toString());print(a.getMessage());print(a.getLocalizedMessage());}
}
}
public static void run(String exe){
try{Process process = Runtime.getRuntime().exec(exe);}catch(Exception e) {
report(e);
}
}
public static boolean ESTACK=true;
public static boolean EMSG=true;
public static int[] reverse(int[] a) {
int temp;
for(int i = 0; i < a.length / 2; i++)
{
temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
return a;
}
public static int[][] reversev(int[][] a) {
/*
* Reverse 2D array
*/
int[] temp;
for(int i = 0; i < a.length / 2; i++)
{
temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
return a;
}
public static int[][] reverseh(int[][] a) {
/*
* Reverse 2D array
*/
int[][] newa=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++) {
newa[i]=reverse(a[i]);
}
return newa;
}
public static int[][] rotate90cw(int[][] map) {
/*
* Rotate 2D array
* 90 degree clockwise
*/
int N=map.length;
int[][] n=new int[map[0].length][N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
n[j][N-1-i]=map[i][j];
}
}
return n;
}
public static int[][] morph(int[][] map,int a,int b){
for(int i=0;i<map.length;i++) {
for(int j=0;j<map[i].length;j++) {
if(map[i][j]==a) {
map[i][j]=b;
}
}
}
return map;
}
public static int classify(char x,char off,char on) {
/*
* Method to classify X is off value or on value
* Returns -1 if neither
*
*/
if (x==off){
return 0;
}else if(x==on) {
return 1;
}else {
return -1;
}
}
public static long slowfib(long num){
//Slow recursion fibonnaci
if(num<=1) {
return num;
}
return slowfib(num-1)+slowfib(num-2);
}
public static ArrayList<Long> fibmem=new ArrayList<Long>();
public static long ffib(long n){
/*
* Fibonnaci implemented with DP
*/
if(n<=1) {
return n;
}
if(fibmem.size()>n) {
return fibmem.get((int) n-1)+fibmem.get((int) n-2);
}else {
fibmem.add(ffib(n-1)+ffib(n-2));
return fibmem.get((int) n);
}
}
public static void print() {
System.out.println();
}
public static void setupfib() {
fibmem.add((long) 0);fibmem.add((long)1);fibmem.add((long)1);fibmem.add((long)2);
}
public static void show2Darr(int[][] a) {
//Print out a 2D array for you
for(int[] b:a) {
for(int c:b) {
print(c+" ","");
}
print();
}
}
public static void showarr(int[] a) {
//Print out a array for you
for(int x:a) {print(x+" ");}
}
public static int[][] dpcache;
public static int ks(int W,int[] wt,int[] val,int n) {
int result;
if(dpcache[n][W]!=0) {return dpcache[n][W];}
if(n==0||W==0) {
result=0;
}else if(wt[n-1]>W) {
result=ks(W,wt,val,n-1);
}else {
result=Math.max(val[n-1]+ks(W-wt[n-1],wt,val,n-1),ks(W,wt,val,n-1));
}
dpcache[n][W]=result;
return result;
}
public static void kssetup(int n,int W) {
dpcache=new int[n+1][W+1];
}
public static int count(int[] arr) {
/*
* Number of groups of 1s
* Modify for other purposes if needed
* Example
* 1111000111
* Returns 2
*
*/
boolean b=false;int c=0;int temp;
for(int i=0;i<arr.length;i++) {
temp=arr[i];
if(temp==0 && b) {
b=false;
c++;
}
if(temp==1 && b==false) {
b=true;
}
}
return c;
}
private static boolean _lock=false;
public static void NOLOCK() {
_lock=true;
}
public static void LOCK() {
if(!(_lock)) {lock=true;}
}
public static void UNLOCK() {
if(!(_lock)) {lock=false;}
}
public static String sha256(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] result = md.digest(input.getBytes());
StringBuffer s = new StringBuffer();
for (int i = 0; i < result.length; i++) {
s.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return s.toString();
}
public static String sha(String input,String type) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(type);
byte[] result = md.digest(input.getBytes());
StringBuffer s = new StringBuffer();
for (int i = 0; i < result.length; i++) {
s.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return s.toString();
}
public static double cos(double a) {return Math.cos(a);}
public static double sin(double a) {return Math.sin(a);}
public static double abs(double a) {return Math.abs(a);}
public static double floor(double a) {return Math.floor(a);}
public static double ceil(double a) {return Math.ceil(a);}
public static void main(String[] args) throws Exception{
System.out.println("Running");
$1();
print("the demo has been removed do to lack of support. Instead we display info about the library.");
$1();
System.out.println($r());
}
public static Queue<Long> speedqueue=new LinkedList<Long>();
public static long prevtime=0;
public static void $1() {
long time=System.currentTimeMillis();
if(prevtime==0) {
prevtime=time;
}else {
speedqueue.add((long) abs(time-prevtime));
prevtime=0;
}
}
public static long $r() {
return speedqueue.poll().longValue();
}
public static boolean $r$smatch(String a,String b) {
return Pattern.matches(a, b);
}
public static boolean $r$match(String a,String b) throws Exception{
//WIP
throw new Exception("Not implemented");
}
}