9_common.ts 883 B

123456789101112131415161718192021222324252627282930
  1. import { ExtractNumbers } from "../common.ts";
  2. /**
  3. * Parse the input for day 9
  4. *
  5. * @param inputArr An array of strings containing numbers
  6. * @returns An array of histograms
  7. */
  8. export function ParseInput(inputArr: string[]): Histogram[] {
  9. return inputArr.map((historyStr) => ExtractNumbers(historyStr) as Histogram);
  10. }
  11. /**
  12. * Find the difference of values in a histogram
  13. *
  14. * @param histogram A array of historical values, or another differential
  15. * @returns A new histogram containing the difference of values in the input histogram
  16. */
  17. export function FindDifferential(histogram: Histogram): Histogram {
  18. const differential: Histogram = [];
  19. for (let i = 1; i < histogram.length; i++) {
  20. differential.push(histogram[i] - histogram[i - 1]);
  21. }
  22. return differential;
  23. }
  24. /** An array of historical environment values */
  25. export type Histogram = number[];