9_1.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { ExtractNumbers, Inspect, LoadInput } from "../common.ts";
  2. const tests = [
  3. "0 3 6 9 12 15",
  4. "1 3 6 10 15 21",
  5. "10 13 16 21 30 45",
  6. ];
  7. const input = await LoadInput(9);
  8. // Parse the histograms from the input
  9. let histograms = ParseInput(input);
  10. // Find the next predicted value for each one
  11. histograms = histograms.map((histogram, idx) => {
  12. try {
  13. return PredictNextValue(histogram);
  14. } catch (err) {
  15. console.error(`Failed on histogram[${idx}]`);
  16. throw err;
  17. }
  18. });
  19. // Sum all of the last values in the histograms
  20. const sumOfNewValues = SumArray(histograms.map((histogram) => histogram[histogram.length - 1]));
  21. // Wrong Answers
  22. // 1987402315 -- too high
  23. console.log(`The sum of all predicted values is: ${sumOfNewValues}`);
  24. function ParseInput(inputArr: string[]): number[][] {
  25. return inputArr.map((historyStr) => ExtractNumbers(historyStr));
  26. }
  27. function PredictNextValue(histogram: number[]): number[] {
  28. const allDifferentials = [histogram];
  29. let differential = [];
  30. // Get all differentials until only an array of zeros remains
  31. do {
  32. differential = FindDifferential(allDifferentials[0]);
  33. allDifferentials.unshift(differential);
  34. } while (SumArray(differential) != 0);
  35. while (allDifferentials.length > 1) {
  36. // Store the last value of previous differential, and remove that differential from the list
  37. const lastValueOfDifferential = allDifferentials.shift()?.pop() as number;
  38. // if (lastValueOfDifferential == undefined) {
  39. // throw new Error("There is no last value of previous differential!\n" + prevDifferential);
  40. // }
  41. // Store the last index of the current differential for easier reading
  42. const lastIndexOfDifferential = allDifferentials[0].length - 1;
  43. // if (lastIndexOfDifferential < 0) {
  44. // throw new Error("There is no last index of the current differential");
  45. // }
  46. // Predict the next value to be this last value + the last value of the previous differential
  47. const predictedValue = allDifferentials[0][lastIndexOfDifferential] + lastValueOfDifferential;
  48. if (Number.isNaN(predictedValue)) {
  49. throw new Error("Failed to find differentials for the following histograms!\n" + histogram);
  50. }
  51. // Push to this differential
  52. allDifferentials[0].push(predictedValue);
  53. }
  54. return allDifferentials[0];
  55. }
  56. function FindDifferential(histogram: number[]): number[] {
  57. const differential: number[] = [];
  58. for (let i = 1; i < histogram.length; i++) {
  59. differential.push(histogram[i] - histogram[i - 1]);
  60. }
  61. return differential;
  62. }
  63. function SumArray(arr: number[]): number {
  64. return arr.reduce((accumulator, value) => accumulator += value, 0);
  65. }