-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
66 lines (55 loc) · 1.58 KB
/
helpers.js
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
const fs = require('fs')
const path = require('path')
const importFile = (dirname, filename = 'input.txt') =>
fs.readFileSync(path.join(dirname, filename), 'utf-8').split('\n')
const unique = arr => [...new Set(arr)]
const cache = (fn, cacheKey) => {
const cacheObj = {}
return (...args) => {
const key = cacheKey(...args)
cacheObj[key] = cacheObj[key] || fn(...args)
return cacheObj[key]
}
}
const _dichotomicSeach = (start, end, getValFn, comparisonFn) => {
while (start <= end) {
const mid = Math.floor((start + end) / 2)
const cmp = comparisonFn(getValFn(mid))
if (cmp < 0) {
end = mid - 1
} else if (cmp > 0) {
start = mid + 1
} else {
return mid
}
}
}
const dichotomicSeach = (range, comparisonFn) => {
range = unique(range)
const index = _dichotomicSeach(
0,
range.length - 1,
i => range[i],
comparisonFn
)
return range[index]
}
const dichotomicSearchInRange = (start, end, comparisonFn) => {
return _dichotomicSeach(start, end, v => v, comparisonFn)
}
const intersection = (arr1, arr2) => arr1.filter(x => arr2.includes(x))
const diff = (arr1, arr2) => arr1.filter(x => !arr2.includes(x))
const sum = (tab, fn = val => val) =>
tab.reduce((sum, el, i) => sum + fn(el, i), 0)
const _default = (el, defaultValue) => (el !== undefined ? el : defaultValue)
module.exports = {
importFile,
unique,
cache,
dichotomicSeach,
dichotomicSearchInRange,
intersection,
diff,
sum,
_default,
}