common.js 889 B

1234567891011121314151617181920212223242526272829
  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. }
  16. /**
  17. * Comparator for sorting numbers into ascending order
  18. *
  19. * @param {number} a
  20. * @param {number} b
  21. *
  22. * @returns {-1|0|1}
  23. */
  24. export function numericAscSort(a, b) {
  25. return a - b;
  26. }