-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
146 lines (130 loc) · 3.11 KB
/
utils.js
File metadata and controls
146 lines (130 loc) · 3.11 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
const R = require('ramda')
/**
* @typedef {[number, number]} Coord
*/
/**
* Split a list in sub lists of given length
* @template T
* @param {number} n
* @param {Array<T>} xs
* @returns {Array<Array<T>>}
*/
const splitList = (n, xs) => (xs.length)
? [xs.slice(0, n)].concat(splitList(n, xs.slice(n)))
: xs
/**
* Find elements that all given arrays have in common
* @template T
* @param {Array<Array<T>>} arrays
* @returns {Array<T>}
*/
const intersection = arrays => [...new Set(arrays[0].filter(
item => arrays.slice(1).reduce(
(acc, curr) => acc && curr.includes(item),
true,
),
))]
/**
* Greatest common divisor
* Grootste gemene deler
*
* on array: numbers.reduce(gcd)
*
* @type {(x:number,y:number) => number}
*/
function gcd (x, y) {
while (y) {
const t = y
y = x % y
x = t
}
return x
}
/**
* Least common multiple
* Kleinste gemene veelvoud
*
* on array: numbers.reduce(lcm)
*
* @type {(x:number,y:number) => number}
*/
function lcm (x, y) {
return Math.abs(x * (y / gcd(x, y)))
}
/**
* Find occurences of str in line
* @param {string} line
* @param {string} str
* @returns {number[]} indices
*/
function findOccurences (line, str) {
const rgx = new RegExp(str, 'gi')
const indices = []
let result = {}
while ((result = rgx.exec(line))) {
indices.push(result.index)
}
return indices
}
/**
* Calculate manhattan distance of two coordinates.
* @param {Coord} coord1
* @param {Coord} coord2
* @returns {number}
*/
const manhattanDistance = ([x1, y1], [x2, y2]) =>
Math.abs(x1 - x2) + Math.abs(y1 - y2)
/** @typedef {any[][]} Matrix */
/**
* Rotate matrix counter clock wise
* @type {(matrix: Matrix) => Matrix}
*/
const rotateL = matrix => matrix[0].map((_, index) => matrix.map(row => row[row.length - 1 - index]))
/**
* Rotate matrix clock wise
* @type {(matrix: Matrix) => Matrix}
*/
const rotateR = matrix => matrix[0].map((_, index) => matrix.map(row => row[index]).reverse())
/**
* Flip matrix over diagonal, from left upper to right lower
* @type {(matrix: Matrix) => Matrix}
*/
const flip = matrix => matrix[0].map((_, index) => matrix.map(row => row[index]))
/** @typedef {ReturnType<newCoordSet>} CoordSet */
/** @param {Map<string, Coord>} coords */
const newCoordSet = (coords) => {
const coordSet = {
/** @type {Map<string, Coord>} */
coords: coords || new Map(),
/** @type {(coord: Coord) => boolean} */
has: coord => coordSet.coords.has(coord.toString()),
/** @type {(coord: Coord) => void} */
add: coord => coordSet.coords.set(coord.toString(), coord),
/** @type {(coord: Coord) => boolean} */
delete: coord => coordSet.coords.delete(coord.toString()),
values: () => coordSet.coords.values(),
clone: () => newCoordSet(new Map(coordSet.coords)),
}
return coordSet
}
function counter (input) {
const C = {}
const grouped = R.groupBy(R.identity, input)
for (const [k, v] of Object.entries(grouped)) {
C[k] = v.length
}
return C
}
module.exports = {
newCoordSet,
splitList,
intersection,
lcm,
gcd,
findOccurences,
manhattanDistance,
rotateL,
rotateR,
flip,
counter,
}