-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
33 lines (31 loc) · 908 Bytes
/
utils.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
const fs = require('fs');
module.exports = {
trimData: data => data.map(item => item.trim()),
calcManhattandistance:
(a, b) => ((Math.abs(a[0]) - Math.abs(b[0])) + (Math.abs(a[1]) - Math.abs(b[1]))),
modDataNewline: (data) => {
const formatData = data.split('\n').map(item => parseInt(item.trim(), 10));
return formatData;
},
modDataNewlineStr: (data) => {
const formatData = data.split('\n').map(item => item.trim());
return formatData;
},
modDataBlanklines: (data) => {
const formatData = data.split(/\n{2,}/g);
return formatData;
},
modDataCommas: (data) => {
const formatData = data.split(',').map(item => parseInt(item.trim(), 10));
return formatData;
},
readInput: (path) => {
try {
const data = fs.readFileSync(path, 'utf8');
return data;
} catch (e) {
console.log('Error:', e.stack);
}
return false;
},
};