Advent of Code 2023-09

 avatar
radulle
javascript
7 months ago
827 B
41
Indexable
Never
function solve(input) {
  return process(parse(input));
}

function parse(input) {
  return input
    .split("\n")
    .map((e) => e.split(" ").map((e) => parseInt(e)))
    .map((e) => [e]);
}

function process(data) {
  for (let i = 0; i < data.length; i++) {
    let k = 1;
    while (true) {
      let max = 0;
      data[i][k] = [];
      for (let j = 1; j < data[i][k - 1].length; j++) {
        data[i][k][j - 1] = data[i][k - 1][j] - data[i][k - 1][j - 1];
        max ||= data[i][k][j - 1];
      }
      if (max === 0) break;
      k++;
    }
  }

  const res = [];
  res[0] = data.map((e) =>
    e.reduce((acc, cur) => acc + cur[cur.length - 1], 0)
  );
  res[1] = data.map((e) => e.reverse().reduce((acc, cur) => cur[0] - acc, 0));

  const sum = res.map((e) => e.reduce((acc, cur) => acc + cur, 0));
  return sum;
}
Leave a Comment