-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.ts
More file actions
1521 lines (1301 loc) · 43.3 KB
/
Copy pathtools.ts
File metadata and controls
1521 lines (1301 loc) · 43.3 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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Md5Hash } from 'https://deno.land/x/checksum@1.4.0/md5.ts';
import { encodeToString } from 'https://deno.land/std@0.53.0/encoding/hex.ts';
export const Digits = [0,1,2,3,4,5,6,7,8,9];
/**
* [start, end)
* if only 1 param is passed start is 0 and param is end
* increment defaults to 1
*/
export function range(start: number, end: number, inc?: number): number[];
export function range(end: number): number[];
export function range(start: number, end?: number, inc: number = 1): number[] {
if(!end){
end = start;
start = 0;
}
return Array.from(
{ length: Math.ceil((end - start) / inc) },
(_, i) => i * inc + start
);
}
/**
* Generates primes less than max
*/
export function sieve(max: number): number[] {
let arr = bools(max);
const count = arr.reduce((acc,el) => el ? acc + 1 : acc, 0); // bc having to make new resized array is slow
const primes = new Array(count);
for(let i = 0, j = 0; i < arr.length; i++) if(arr[i]) primes[j++] = i;
return primes;
}
/**
* Generates a boolean array where arr[i] = isPrime(i)
*/
export function bools(max: number): boolean[] {
let arr = new Array(max).fill(true);
arr[0] = arr[1] = false;
for(let i = 2; i <= Math.sqrt(max); i++){
if(arr[i]) for(let j = i * i; j < max; j += i) arr[j] = false;
}
return arr;
}
export function mod(n: number, t: number){
if(n>=0) return n%t;
else return (t+(n%t))%t;
}
export function sudokuSolve(b: number[][]): number[][] | undefined {
function solve(b: number[][]){
let x: number;
let y = -1;
for(x = 0; x < 9 && y == -1; x++){
y = b[x].indexOf(0);
} x--;
if(x == 8 && y == -1) return b;
else {
let cs = sudokuChoices(b, x, y);
cs.forEach(c=>{
let nb = b.map(arr=>arr.slice());
nb[x][y]=c;
let sol = solve(nb);
if(sol) return sol;
});
return undefined;
}
}
return solve(b);
}
export function sudokuChoices(arr: number[][], x: number, y: number){
let seen = new Set(Digits.slice(1));
for(let i = 0; i < 9; i++){
seen.delete(arr[x][i]);
seen.delete(arr[i][y]);
seen.delete(arr[Math.floor(i/3)+3*Math.floor(x/3)][(i%3)+3*Math.floor(y/3)]);
}
return [...seen];
}
export const sum = (nums: number[]) => nums.reduce((a, n) => a + n, 0);
export const sumBigInt = (nums: bigint[]) => nums.reduce((a, n) => a + n, 0n);
export const sumVec2 = (nums: Vec2[]) => nums.reduce((a, n) => {
a.x += n.x;
a.y += n.y;
return a;
}, new Vec2(0));
export const sumVec3 = (nums: Vec3[]) => nums.reduce((a, n) => {
a.x += n.x;
a.y += n.y;
a.z += n.z;
return a;
}, new Vec3(0));
export const product = (nums: number[]) => nums.reduce((a, n) => a * n, 1);
export const productBigInt = (nums: bigint[]) => nums.reduce((a, n) => a * n, 1n);
/**
* PLEASE PASS PRIMES TO THIS OR MEGA SLOW
*/
export const primeFactors = (n: number, given: number[] | null = null) => {
const primes = given || sieve(n);
let runningTotal = n;
const factors: number[] = [];
let i = 0;
while(runningTotal !== 1 && i < primes.length){
if(runningTotal % primes[i] === 0){
factors.push(primes[i]);
runningTotal /= primes[i];
}
else i++;
}
if(runningTotal !== 1) factors.push(runningTotal);
return factors;
}
export const groupBy = <T, K>(arr: T[], pred: (arg: T) => K): DefaultMap<K, T[]> => {
const map = new DefaultMap<K, T[]>(() => []);
for(const val of arr){
const computed = pred(val);
map.get(computed).push(val);
}
return map;
}
export const countRepeats = <T>(arr: T[]): Map<T, number> => {
const map = new Map();
for(const elem of arr){
if(!map.has(elem)) map.set(elem, 1);
else map.set(elem, map.get(elem) + 1);
}
return map;
}
export const intDiv = (a: number, b: number) => Math.floor(a/Number(b));
export const digitsOf = (n: number, pad: number | null = null) => {
if(n<0) throw new Error('Invalid argument, number must be positive');
const digits: number[] = [];
while(n > 0){
digits.push(n % 10);
n = intDiv(n, 10);
}
if(pad !== null) while(digits.length < pad) digits.push(0);
return digits;
}
export const digitsOfBigInt = (n: bigint, pad: number | null = null) => {
if(n < 0) throw new Error('Invalid argument, number must be positive');
const digits: bigint[] = [];
while(n > 0){
digits.push(n % 10n);
n /= 10n;
}
if(pad !== null) while(digits.length < pad) digits.push(0n);
return digits;
}
export const isPalindrome = (n: number) => {
const digits = digitsOf(n);
for(let i = 0; i < digits.length / 2; i++) if(digits[i] !== digits[digits.length - i - 1]) return false;
return true;
}
export const isPalindromeBigInt = (n: bigint) => {
const digits = digitsOfBigInt(n);
for(let i = 0; i < digits.length / 2; i++) if(digits[i] !== digits[digits.length - i - 1]) return false;
return true;
}
export const numberFromDigits = (digits: number[]) => sum(digits.map((n, i) => n * 10 ** i));
export const bigIntFromDigits = (digits: bigint[]) => sumBigInt(digits.map((n, i) => n * 10n ** BigInt(i)));
export const reverseNumber = (n: number) => numberFromDigits(digitsOf(n).reverse());
export const reverseBigInt = (n: bigint) => bigIntFromDigits(digitsOfBigInt(n).reverse());
export const combos = function*<T>(arr: T[], times: number): Generator<T[]> {
function* helper(index: number, result: T[]): Generator<T[]> {
if(index === times - 1){
for(const elem of arr) {
result[index] = elem;
yield result;
}
}else{
for(const elem of arr){
result[index] = elem;
yield* helper(index + 1, result);
}
}
}
yield* helper(0, new Array(times));
}
export const nonRepeatingCombos = function*<T>(arr: T[], times: number): Generator<T[]> {
function* helper(index: number, start: number, result: T[]): Generator<T[]> {
if(index === times - 1){
for(let i = start; i < arr.length; i++){
result[index] = arr[i];
yield result;
}
}else{
for(let i = start; i < arr.length; i++){
const elem = arr[i];
result[index] = elem;
yield* helper(index + 1, i + 1, result);
}
}
}
yield* helper(0, 0, new Array(times));
}
export const combosWithLowerTimes = function*<T>(arr: T[], times: number): Generator<T[]>{
for(let i = times; i > 0; i--){
yield* combos(arr,i);
}
}
export const nonRepeatingCombosWithLowerTimes = function*<T>(arr: T[], times: number): Generator<T[]>{
for(let i = times; i > 0; i--){
yield* nonRepeatingCombos(arr,i);
}
}
export const memoize = <P, R>(fn: ((arg0: P) => R), defaults?: Map<P, R>): ((arg0: P) => R) => {
const cache = defaults || new Map<P,R>();
return arg => {
if(cache.has(arg)) return cache.get(arg) as R;
const result = fn(arg);
cache.set(arg, result);
return result;
}
}
export const factors = (n: number) => {
const factors = [1];
for(let i = 2; i < Math.ceil(Math.sqrt(n)); i++){
if(Number.isInteger(n/i)) factors.push(i, n/i);
}
return factors;
}
export const zip = <R extends any[][]>(
...arrs: R
): { [K in keyof R]: R[K] extends (infer T)[] ? T : never }[] =>
range(
Math.min(...arrs.map(a => a.length))
).map(i => arrs.map(a => a[i])) as any;
export const zipWith = <F, R extends any[][]>(
mapper: (args: { [K in keyof R]: R[K] extends (infer T)[] ? T : R[K] }) => F,
...arrs: R
): F[] => zip(...arrs).map(group => mapper(group as any));
export const readFile = (path: string) => (new TextDecoder("utf-8")).decode(Deno.readFileSync(path)).replaceAll('\r','');
export const isCoprime = (a: number, b: number) => {
for(let i = 2; i <= Math.min(a, b); i++) if(b % i === 0 && a % i === 0) return false;
return true;
}
export const totient = (n: number) => {
let counter = 0;
for(const num of range(1,n)) {
if(isCoprime(n, num)) counter++;
}
return counter;
}
export const newTotient = (n: number, given?: number[]) => {
const primes = given || sieve(n);
let t = n;
for(const p of primes){
if(n % p === 0) t *= 1 - 1/p;
}
if(t === n) t--;
return Math.round(t);
}
/**
* totientsUpTo(n)[i <= n] = phi(i)
*/
export const totientsUpTo = (target: number) => {
const phi = range(0,target+1);
for(let n = 2; n <= target; n++){
if(phi[n] == n){ // this number is prime
phi[n]--; // phi(prime) = prime - 1
for(let i = n * 2; i <= target; i += n) // loop through multiples of this number
phi[i] *= 1 - (1 / n); //removes the stuff
}
}
return phi;
}
/**
* outputs totients starting from phi(i), phi(2) ... phi(target)
*/
export const totientsUpToGenerator = function*(target: number){
const phi = range(0,target+1);
yield 1;
for(let n = 2; n <= target; n++){
if(phi[n] == n){ // this number is prime
phi[n]--; // phi(prime) = prime - 1
for(let i = n * 2; i <= target; i += n) // loop through multiples of this number
phi[i] *= 1 - (1 / n); //removes the stuff
}
yield phi[n];
}
return phi;
}
export const debugDecorator = (label: string) => <T extends any[], R>(fn: ((...args: T) => R)) => (...args: T) => {
console.log(`called`, label, 'args', args)
const result = fn(...args)
console.log(`returned from`, label, result, 'args', args)
return result;
}
export const factorial = (n: number) => range(2,n+1).reduce((a,c) => a * c, 1)
/**
* Example: f([[1,2],[3,4]]) -> [1,3] [1,4] [2,3] [2,4]
* needs better typing
*/
export const multiSampleCombos = function*<T extends any[]>(
arrs: T[]
): Generator<T> {
const [first, ...rest] = arrs;
if(!first) yield [] as unknown as T;
else for(const combo of multiSampleCombos(rest)){
for(const elem of first){
yield [elem, ...combo] as T;
}
}
}
export const naturalNumGenerator = function*(){
for(let i = 1; true; i++) yield i;
}
export const takeFromIterator = <T>(
gen: Iterator<T>,
amount: number
): T[] => {
const items = new Array(amount);
for(let i = 0; i < amount; i++) items[i] = gen.next().value;
return items;
}
export const takeAll = <T>(itr: Iterator<T>) => {
const items: T[] = [];
while(true){
const value = itr.next();
if(value.done) break;
items.push(value.value);
}
return items;
}
export function* mapGenerator<T, R>(gen: Generator<T>, fn: ((value: T) => R)): Generator<R> {
while(true){
const value = gen.next();
if(value.done) return;
yield fn(value.value)
}
}
export function* zipGenerators<T extends Generator<any>[]>(
...gens: T
): (Generator<{ [K in keyof T]: T[K] extends (Generator<infer S>) ? S : T[K] }>){
return function*(){
while(true){
const values = gens.map(gen => gen.next());
if(values.some(value => value.done)) return;
yield values.map(value => value.value) as any;
}
}
}
export function* zipIterables<T extends Iterable<any>[]>(
...args: T
): Iterable<{ [K in keyof T]: T[K] extends Iterable<infer U> ? U : T[K] }> {
const iterators = args.map((x) => x[Symbol.iterator]());
while (true) {
const next = iterators.map((i) => i.next());
if (next.some((v) => v.done)) break;
yield next.map((v) => v.value) as any;
}
}
export function* filterGenerator<T>(gen: Generator<T>, pred: ((value: T) => boolean)): Generator<T> {
while(true){
const value = gen.next();
if(value.done) return;
if(!pred(value.value)) continue;
yield value.value
}
}
export function* prependConstants<T>(gen: Generator<T>, values: T[]): Generator<T> {
for(const value of values) yield value;
yield* gen;
}
export const id = <T>(x: T) => x;
export function succ(x: number): number;
export function succ(x: bigint): bigint;
export function succ(x: number | bigint) {
if(typeof x === 'number') return x + 1;
if(typeof x === 'bigint') return x + 1n;
throw new Error('Successor is not defined for this type');
};
export const binSearch = <T>(
arr: T[],
target: T,
accessor?: ((arg: T) => number),
start?: number,
end?: number,
): number => {
let kStart = start ?? 0;
let kEnd = end ?? arr.length;
if(!accessor){
if(typeof arr[0] === 'number'){
accessor = accessor ?? id as ((arg: T) => number);
} else {
throw Error('binSearch on non number arrays requires and accessor');
}
}
const accessedTarget = accessor(target);
while(kStart <= kEnd){
let mid = kStart + Math.floor((kEnd - kStart) / 2);
const accessedMid = accessor(arr[mid]);
if(accessedMid === accessedTarget) return mid;
else if(accessedMid < accessedTarget) kStart = mid + 1;
else kEnd = mid - 1;
}
return kStart - 1;
}
/**
* checks in number is in (lower, upper) or if the inclusive parameter is sent then [lower, upper]
*/
export const inRange = (n: number, lower: number, upper: number, inclusive: boolean = false) => {
if(inclusive) return lower <= n && n <= upper;
return lower < n && n < upper;
}
/**
* [lower, upper)
*/
export const inRangeStd = (n: number, lower: number, upper: number) => lower <= n && n < upper;
export const countMatches = <T>(arr: T[], pred: ((arg: T, index: number) => boolean)) => arr.reduce((acc, current, index) => pred(current, index) ? acc + 1 : acc, 0);
export const union = <T>(...sets: Set<T>[]): Set<T> => new Set(sets.map(s => [...s]).flat(1));
export const intersection = <T>(a: Set<T>, ...rest: Set<T>[]) => new Set<T>(takeAll(a.values()).filter(v => rest.every(set => set.has(v))));
export const subtractSets = <T>(a: Set<T>, b: Set<T>, destructive: boolean = false): Set<T> => {
const base = destructive ? a : new Set(a);
for(const val of b) base.delete(val);
return base;
}
/** is `a` a subset of `b` */
export const isSubset = (a: Set<any>, b: Set<any>): boolean => {
for(const elem of a) if(!b.has(elem)) return false;
return true;
}
export const pickIntsFromString = (str: string) => str.match(/-?\d+/g)?.map(s => parseInt(s)) ?? [];
export class MultiMap<Ks extends any[], T> implements Map<Ks, T>{
numKeys: number;
root: Map<any, any>;
size: number;
constructor(numKeys: number){
this.numKeys = numKeys;
this.root = new Map();
this.size = 0;
}
get(keys: Ks): T | undefined {
if(keys.length !== this.numKeys) return;
let current = this.root;
for(let layer = 0; layer < this.numKeys; layer++){
if(!current.has(keys[layer])) return undefined;
current = current.get(keys[layer]);
}
return current as any as T;
}
set(keys: Ks, value: T) {
if(keys.length !== this.numKeys) return this;
let current = this.root;
for(let layer = 0; layer < this.numKeys - 1; layer++){
if(!current.has(keys[layer])) current.set(keys[layer], new Map())
current = current.get(keys[layer]);
}
if(!current.has(keys[this.numKeys - 1])) this.size++;
current.set(keys[this.numKeys - 1], value);
return this;
}
has(keys: Ks){
if(keys.length !== this.numKeys) return false;
let current = this.root;
for(let layer = 0; layer < this.numKeys - 1; layer++){
if(!current.has(keys[layer])) return false;
current = current.get(keys[layer]);
}
return current.has(keys[this.numKeys - 1]);
}
*entries(): Generator<[Ks, T]> {
const self = this;
function* yieldFrom(prepend: any, map: Map<any, any>, layer: number): Generator<[any, T]> {
if(layer === self.numKeys) {
for(const [key, entry] of map.entries()){
yield [[...prepend, key], entry]
}
} else {
for(const [key, entry] of map.entries()){
yield* yieldFrom([...prepend, key], entry, layer + 1);
}
}
}
yield* yieldFrom([], this.root, 1);
}
*keys(): Generator<Ks> {
for(const [keys, _] of this.entries()) yield keys;
}
*values(): Generator<T> {
for(const [_, value] of this.entries()) yield value;
}
clear(){
this.root = new Map();
this.size = 0;
}
delete(keys: Ks) {
if(keys.length !== this.numKeys) return false;
let current = this.root;
for(let layer = 0; layer < this.numKeys - 1; layer++){
if(!current.has(keys[layer])) return false;
current = current.get(keys[layer]);
}
const didDelete = current.delete(keys[this.numKeys - 1]);
if(didDelete) this.size--;
return didDelete;
}
forEach(cb: (value: T, keys: Ks, map: this) => void) {
for(const [keys, value] of this.entries()){
cb(value, keys, this);
}
}
get [Symbol.toStringTag](){
return 'MultiMap';
}
*[Symbol.iterator](){
yield* this.entries();
}
/** does nothing if key does not exist */
apply(key: Ks, fn: (val: T, key: Ks) => T){
if(this.has(key)) this.set(key, fn(this.get(key) as T, key));
return this;
}
}
export class DefaultMultiMap<K extends any[], V> extends MultiMap<K, V>{
derive: (key: K) => V;
constructor(keys: Sizes, deriveDefault: (key: K) => V){
super(keys);
this.derive = deriveDefault;
}
get(key: K): V {
const stored = super.get(key);
if(stored === undefined) return this.derive(key);
return stored;
}
apply(key: K, fn: (val: V, key: K) => V): this {
this.set(key, fn(this.get(key), key));
return this;
}
has(key: K){
return true;
}
}
export const memoizeMulti = <Ks extends any[], R>(fn: (...args: Ks) => R, defaults?: MultiMap<Ks, R>): ((...args: Ks) => R) => {
const cache = defaults || new MultiMap<Ks, R>(fn.length);
return (...args: Ks) => {
if(cache.has(args)) return cache.get(args)!;
const result = fn(...args);
cache.set(args, result);
return result;
}
}
export const enumerate = <T>(arr: T[]): [value: T, index: number][] => arr.map((v, i) => [v, i]);
export class DefaultMap<K, V> extends Map<K, V>{
constructor(private derive: (key: K) => V){
super();
}
get(key: K): V {
const stored = super.get(key);
if(stored === undefined) {
const computed = this.derive(key);
this.set(key, computed)
return computed;
}
return stored;
}
apply(key: K, fn: (val: V, key: K) => V): this {
this.set(key, fn(this.get(key), key));
return this;
}
has(key: K){
return true;
}
}
export const takeFirst = <T>(iter: Iterator<T>) => iter.next().value;
export function* subSequences<T>(arr: T[], minLength = 1, maxLength = Infinity): Generator<T[]> {
if(minLength === 0){
yield [];
minLength = 1;
}
for(let offset = 0; offset <= arr.length - minLength; offset++){
for(let length = minLength; length <= Math.min(arr.length - offset, maxLength); length++){
yield arr.slice(offset, offset + length);
}
}
}
export function* subSequencesOfSize <T>(arr: T[], size: number): Generator<T[]> {
for(let offset = 0; offset < arr.length - size; offset++){
yield arr.slice(offset, offset + size);
}
}
export const iteratorSome = <T>(iter: Iterator<T>, pred: (arg: T) => boolean) => {
while(true){
const { done, value } = iter.next();
if(done) return false;
if(pred(value)) return true;
}
}
export const concat = <T>(arrs: T[][]): T[] => arrs.reduce((acc, cur) => [...acc, ...cur], []);
export const deepCopyArray = <T extends any[]>(arr: T): T => arr.map(elem => Array.isArray(elem) ? deepCopyArray(elem) : elem) as T;
export type Sizes = 1 | 2 | 3 | 4 | 5;
export type MultiDimArray<D extends Sizes, T> = [T, T[], T[][], T[][][], T[][][][], T[][][][][]][D];
export type TupleSizes<D extends Sizes, T> = [[], [T], [T,T], [T,T,T], [T,T,T,T], [T,T,T,T,T]][D];
export class Grid<D extends Sizes, T>{
private internal: MultiDimArray<D, T>;
dimensions: D;
sizes: TupleSizes<D, number>;
OUT_OF_BOUNDS = {};
constructor(dimensions: D, data: MultiDimArray<D, T>, skipCopy: boolean = false){
this.internal = skipCopy ? data : deepCopyArray(data);
this.dimensions = dimensions;
const sizes: number[] = [];
let prev: any = data;
for(let d = 0; d < dimensions; d++) {
sizes.push(prev.length)
prev = prev[0];
}
this.sizes = sizes as TupleSizes<D, number>;
}
*neighborOffsets(): Generator<TupleSizes<D, number>> {
yield* combos([-1,0,1], this.dimensions) as Generator<TupleSizes<D, number>>;
}
private getHelper(arr: any, coords: number[], idx: number = 0, wrapAround: boolean = true): T[] | T | Grid<D,T>["OUT_OF_BOUNDS"] {
if(arr === undefined) return this.OUT_OF_BOUNDS;
if(coords.length === idx) return arr;
return wrapAround ?
this.getHelper(arr[mod(coords[idx], arr.length)], coords, idx + 1, wrapAround) :
this.getHelper(arr[coords[idx]], coords, idx + 1, wrapAround);
}
get(coords: TupleSizes<D, number>, wrapAround: boolean = false): T | Grid<D,T>["OUT_OF_BOUNDS"] {
return this.getHelper(this.internal, coords, 0, wrapAround);
}
set(coords: TupleSizes<D, number>, value: T): this {
const arr = this.getHelper(this.internal, coords.slice(0, -1)) as T[];
if(arr === this.OUT_OF_BOUNDS) return this;
arr[mod(coords[coords.length - 1], arr.length)] = value;
return this;
}
*castRay(
origin: TupleSizes<D, number>,
velocity: TupleSizes<D, number>,
wrapAround: boolean = false
): Generator<[coords: TupleSizes<D, number>, data: T]> {
for(let d = 1; true; d++){
let current: TupleSizes<D, number> = zipWith(sum, origin as number[], velocity.map(n => n * d)) as TupleSizes<D, number>;
if(!wrapAround && current.some((n, d) => inRangeStd(n, 0, this.sizes[d]))) return;
yield [current, this.get(current, true) as any];
}
}
neighbors(coords: TupleSizes<D, number>, wrapAround: boolean = false): [coords: TupleSizes<D, number>, data: T][]{
const neighbors: [coords: TupleSizes<D, number>, data: T][] = [];
for(const offset of this.neighborOffsets()){
const specific = zipWith(sum, coords as number[], offset as number[]) as TupleSizes<D, number>;
if(zipWith(([a,b]) => a === b, specific, coords).every(id)) continue;
const data = this.get(specific, wrapAround);
if(data !== this.OUT_OF_BOUNDS) neighbors.push([specific, data as any]);
}
return neighbors;
}
clone(): Grid<D, T> {
return new Grid(this.dimensions, this.internal);
}
*entries(lazy = true): Generator<[coords: TupleSizes<D, number>, data: T]> {
function* helper(arr: any[], coords: number[], dim: number): Generator<any> {
for(let i = 0; i < arr.length; i++){
const elem = arr[i];
coords[dim] = i;
if(Array.isArray(elem)) yield* helper(elem, coords, dim + 1);
else yield [lazy ? coords : coords.slice(0), elem];
}
}
yield* helper(this.internal, new Array(this.dimensions), 0);
}
keys(lazy = true): Generator<TupleSizes<D, number>> {
return mapGenerator(this.entries(lazy), ([coords, _]) => coords);
}
*values(): Generator<T> {
function* helper(arr: any[]): Generator<any> {
for(let i = 0; i < arr.length; i++){
const elem = arr[i];
if(Array.isArray(elem)) yield* helper(elem);
else yield elem;
}
}
yield* helper(this.internal);
}
forEach(callback: (value: T, indicies: TupleSizes<D, number>, self: this) => void, lazy = true): this {
for(const [index, value] of this.entries(lazy)){
callback(value, index, this);
}
return this;
}
map<R>(callback: (value: T, indicies: TupleSizes<D, number>, self: this) => R, lazy = true): Grid<D, R> {
const mapper = (arr: any[], coords: number[], dim: number): any => arr.map((elem, index) => {
coords[dim] = index;
if(Array.isArray(elem)) return mapper(elem, coords, dim + 1);
return callback(elem as T, (lazy ? coords : coords.slice(0)) as TupleSizes<D, number>, this)
})
return new Grid<D, R>(this.dimensions, mapper(this.internal, new Array(this.dimensions), 0), true);
}
reduce<R>(reducer: (acc: R, value: T, indicies: TupleSizes<D, number>, self: this) => R, initial: R, lazy = true){
let acc = initial;
for(const [coords, value] of this.entries(lazy)){
acc = reducer(acc, value, coords, this);
}
return acc;
}
asArray(copy: boolean = false): MultiDimArray<D, T> {
return copy ? deepCopyArray(this.internal) : this.internal;
}
equals(other: Grid<Sizes, any>){
if(this.dimensions !== other.dimensions) return false;
for(const [a, b] of zipGenerators(this.values(), other.values())){
if(a !== b) return false;
}
return true;
}
}
export const first = <T>(arg: readonly [T, ...any]): T => arg[0];
export const second = <T>(arg: readonly [any, T, ...any]): T => arg[1];
export const third = <T>(arg: readonly [any, any, T, ...any]): T => arg[2];
export function add(x: number): (y: number) => number;
export function add(x: bigint): (y: bigint) => bigint;
export function add(x: string): (y: string | number | bigint) => string;
export function add(x: Vec2): (y: Vec2) => Vec2;
export function add(x: Vec3): (y: Vec3) => Vec3;
export function add<T>(x: Set<T>): (y: Set<T>) => Set<T>;
export function add<T>(x: number | bigint | string | Vec2 | Vec3 | Set<T>) {
if(typeof x === 'number') return (y: number) => x + y;
if(typeof x === 'bigint') return (y: bigint) => x + y;
if(typeof x === 'string') return (y: string | number | bigint) => x + y;
if(x instanceof Vec2) return (y: Vec2) => x.add(y);
if(x instanceof Vec3) return (y: Vec3) => x.add(y);
if(x instanceof Set) return (y: Set<T>) => union(x, y);
throw new Error('add is not defined for this type');
}
export type Vec2Str = ReturnType<Vec2['toString']>
export class Vec2 {
x: number;
y: number;
static ORIGIN = new Vec2(0);
static SIDES = [
new Vec2(0, 1),
new Vec2(1, 0),
new Vec2(0, -1),
new Vec2(-1, 0),
]
static DIAGONALS = [
new Vec2(1, 1),
new Vec2(1, -1),
new Vec2(-1, 1),
new Vec2(-1, -1),
]
constructor(x: number, y?: number){
this.x = x;
this.y = y ?? x;
}
add(otr: Vec2): Vec2 {
return new Vec2(this.x + otr.x, this.y + otr.y)
}
mult(scalar: number): Vec2 {
return new Vec2(this.x * scalar, this.y * scalar)
}
sub(otr: Vec2): Vec2 {
return this.add(otr.mult(-1));
}
div(scalar: number): Vec2 {
return this.mult(1 / scalar);
}
len(){
return (this.x ** 2 + this.y ** 2) ** 0.5;
}
manhattenLen(){
return Math.abs(this.x) + Math.abs(this.y)
}
minDist(otr: Vec2){
return Math.min(Math.abs(this.x - otr.x), Math.abs(this.y - otr.y))
}
eq(otr: Vec2, EPSILON = 0.00001){
return Math.abs(this.x - otr.x) < EPSILON && Math.abs(this.y - otr.y) < EPSILON;
}
normalized(): Vec2 {
return this.div(this.len())
}
angle(): number {
return Math.atan2(this.y, this.x);
}
toString(): `${number},${number}` {
return `${this.x},${this.y}`;
}
static read(s: string): Vec2 {
return new Vec2(...s.split(',').map(s => Number(s)) as [number, number]);
}
get<T>(source: T[][]): T | undefined {
return source[this.x]?.[this.y];
}
}
export type Vec3Str = ReturnType<Vec3['toString']>
export class Vec3{
x: number;
y: number;
z: number;
static ORIGIN = new Vec3(0);
constructor(x: number, y?: number, z?: number){
this.x = x;
this.y = y ?? x;
this.z = z ?? x;
}
add(otr: Vec3): Vec3 {
return new Vec3(this.x + otr.x, this.y + otr.y, this.z + otr.z)
}
mult(scalar: number): Vec3 {
return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar)
}
sub(otr: Vec3): Vec3 {
return this.add(otr.mult(-1));
}
div(scalar: number): Vec3 {
return this.mult(1 / scalar);
}
len(){
return (this.x ** 2 + this.y ** 2 + this.z ** 2) ** 0.5;
}
manhattenLen(){
return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z)
}
minDist(otr: Vec3){
return Math.min(Math.abs(this.x - otr.x), Math.abs(this.y - otr.y), Math.abs(this.z - otr.z))
}
eq(otr: Vec3, EPSILON = 0.00001){
return Math.abs(this.x - otr.x) < EPSILON && Math.abs(this.y - otr.y) < EPSILON && Math.abs(this.z - otr.z) < EPSILON;
}
normalized(): Vec3 {
return this.div(this.len())
}
toString(): `${number},${number},${number}` {
return `${this.x},${this.y},${this.z}`;
}
static read(s: string): Vec3 {
return new Vec3(...s.split(',').map(s => Number(s)) as [number, number, number]);
}
get<T>(source: T[][][]): T | undefined {
return source[this.x]?.[this.y]?.[this.z];
}
}
export function group<T>(arr: T[], size: 2): [T, T][]
export function group<T>(arr: T[], size: 3): [T, T, T][]
export function group<T>(arr: T[], size: 4): [T, T, T, T][]
export function group<T>(arr: T[], size: 5): [T, T, T, T, T][]
export function group<T>(arr: T[], size: number): T[][]
export function group<T>(arr: T[], size: number): T[][] {
const result: T[][] = [];
for(let i = 0; i <= arr.length - size; i += size) result.push(arr.slice(i, i + size));
return result;
}
export const uint8ToHex = (()=>{
const hexTable = new TextEncoder().encode('0123456789abcdef');
const decoder = new TextDecoder();
return (data: Uint8Array) => {
const out = new Uint8Array(data.length * 2);
for(let i = 0; i < data.length; i++){
out[i * 2] = hexTable[data[i] >> 4]
out[i * 2 + 1] = hexTable[data[i] & 0x0F]
}
return decoder.decode(out);
}
})();