common.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }
  27. /**
  28. * Convert a string to an integer
  29. *
  30. * Does no checks to make sure the value is actually a number-like value
  31. *
  32. * @param {string} string
  33. *
  34. * @returns {number}
  35. */
  36. export function strToInt(string) {
  37. return parseInt(string, 10);
  38. }