-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
106 lines (92 loc) · 2.87 KB
/
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
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
const fs = require('promise-fs');
const fetch = require('node-fetch');
const envPath = require('find-config')('.env');
// dotenv is trying to read .env from the path which calls utils.js, so the path is NOT relative to utils.js
require('dotenv').config({ path: envPath });
/**
* Try to figure out the date to use
* @returns {*[]}
*/
const guessDate = () => {
// If no date is specified, try to guess the date from the path of the calling file
let [, year, day] = process.cwd().match(/(\d{4})\/(\d{1,2})$/) || [];
if (day && day.startsWith('0')) {
day = day.substr(1);
}
// In case we still don't have a date try to guess it from today's day
const today = new Date();
if (!year) {
const month = today.getMonth();
year = today.getFullYear();
// If we are not in december, let's assume that it is last years puzzle we are interested in
if (month < 11) {
year -= 1;
}
}
if (!day) {
day = today.getDate();
}
return [year, day];
};
const getRemoteData = ({ year, day } = {}) => {
if (!year && !day) {
[year, day] = guessDate();
}
const session = process.env.SESSION;
if (!session) {
throw new Error(`No session id specified.
Please update your .env file with your session id from the cookie at adventofcode.com`);
}
const url = `https://adventofcode.com/${year}/day/${day}/input`;
const headers = {
cookie: `session=${session}`,
};
return fetch(url, { headers }).then((res) => res.text());
};
const getData = async () => {
const fileName = 'input.txt';
try {
return await fs.readFile(fileName, 'ascii');
} catch (err) {
if (process.env.JEST_WORKER_ID === undefined) {
console.log('No local input file found. Calling adventofcode.com');
const content = await getRemoteData();
await fs.writeFile(fileName, content);
return content;
}
throw new Error('Trying to download data when running Jest');
}
};
const getRows = () => getData().then((data) => data.trim().split('\n'));
const getRow = () => getRows().then((data) => data[0]);
const getNumberRows = () => getRows().then((data) => data.map(Number));
const clone = (object) => JSON.parse(JSON.stringify(object));
const grouped = (arr, groupSize) =>
arr.reduce((acc, item, index) => {
// eslint-disable-next-line no-unused-expressions
index % groupSize === 0 ? acc.push([item]) : acc[acc.length - 1].push(item);
return acc;
}, []);
const minBy = (cb) => (a, b) => (cb(b) < cb(a) ? b : a);
const groupRows = (rows) =>
rows.reduce(
(acc, row) => {
// eslint-disable-next-line no-unused-expressions
row ? acc[acc.length - 1].push(row) : acc.push([]);
return acc;
},
[[]],
);
const minInArray = (arr) => Math.min(...arr);
const maxInArray = (arr) => Math.max(...arr);
module.exports = {
getRow,
getRows,
getNumberRows,
clone,
grouped,
minBy,
minInArray,
maxInArray,
groupRows,
};