-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPuzzleInput.js
More file actions
37 lines (33 loc) · 1.21 KB
/
getPuzzleInput.js
File metadata and controls
37 lines (33 loc) · 1.21 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
import { getInput } from '../api/index.js';
import { cacheInput, getCachedInput, inputIsCached } from './inputCache.js';
import { inputIsValid } from '../validation/validateInput.js';
import { getAuthToken } from '../persistence/metaRepository.js';
import { InvalidPuzzleInputError } from '../errors/puzzleErrors.js';
import { logger } from '../logger.js';
/**
* Downloads the input file and saves it to the inputs folder.
* @returns {Promise<string>}
*/
const downloadAndCacheInput = async (year, day) => {
const input = await getInput(year, day, getAuthToken());
await cacheInput(year, day, input);
return input;
};
/**
* Returns the input for the puzzle.
* If the input is cached, the cached input will be returned.
* If the input is not cached, it will be downloaded and cached
* @param {number} year
* @param {number} day
* @returns {Promise<string>}
*/
export const getPuzzleInput = async (year, day) => {
logger.debug('loading input file for puzzle year: %s, day: %s', year, day);
const input = (await inputIsCached(year, day))
? await getCachedInput(year, day)
: await downloadAndCacheInput(year, day);
if (!inputIsValid(input)) {
throw new InvalidPuzzleInputError();
}
return input;
};