common.js 697 B

1234567891011121314151617
  1. import fs from "node:fs";
  2. /**
  3. * Load the input for a day's challenge
  4. *
  5. * @param {string|number} dayNumber The number of the day to load. A string for a file path can be provided here too.
  6. * @param {Function} parsingFunction A function to parse the input to a usable format
  7. * @param {boolean} literalPath If true, the {@link dayNumber} value should be used as a file path
  8. *
  9. * @returns {unknown}
  10. */
  11. export function loadInput(dayNumber, parsingFunction, literalPath = false) {
  12. let filePath = literalPath ? dayNumber : `./inputs/day_${dayNumber}.input`;
  13. const data = fs.readFileSync(filePath, "utf8");
  14. return typeof parsingFunction == "function" ? parsingFunction(data) : data;
  15. }