---
title: "Advent of Code 2023: Day Nine"
date: 2023-12-09T07:49:18
slug: advent-of-code-2023-day-nine
tags: [advent-of-code-2023]
---
On to Day Nine of [Advent of Code](https://adventofcode.com). As always, the code is available [on Git](https://git.lewisdale.dev/lewis/advent-of-code-2023), and the other posts are under the [#AdventOfCode2023 tag](https://lewisdale.dev/post/tag/advent-of-code-2023).

## Part One

Something something exposition. We've got a list of lists of numbers that represent how a value changes over time, and we have to calculate the next value in the sequence. 

We do this by finding the difference between each values, until the differences are all zeroes, and then working out the next value in order, e.g. `0, 3, 6, 9, 12, 15` becomes `3, 3, 3, 3, 3, 3`, which is then `0, 0, 0, 0`. So the next value in the `3's` list is `3+0`, and the next value in the original sequence is `15+3`, so `18`.

My function to get the next value in the sequence is pretty simple. Really, we don't need to work out when it becomes all zeroes, just when every value is the same. So I apply the function recursively, and add the result to the last value in the sequence:

```javascript
getNextValueInSequence(seq: number[]): number {
        if (new Set(seq).size === 1) return seq[0];
        const differences = seq.reduce((acc: number[], curr, i, arr) => {
            if (i === 0) return acc;
            acc.push(curr - arr[i - 1]);
            return acc;
        }, []);
        return this.getNextValueInSequence(differences) + seq[seq.length - 1];
}
```

Then to calculate the totals I just parse the input (which is simple enough that I didn't bother with using `parjs` this time, I just split each line by `' '`, and map `Number` over the result). Then I call `reduce` over the inputs with `getNextValueInSequence` and add the results:

```javascript
export class Oasis {
    private readonly sequences: number[][];

    constructor(input: string) {
        this.sequences = input.split('\n').map(line => line.split(' ').map(Number));
    }
	
	public getTotalOfNextValuesInSequences(): number {
        return this.sequences.reduce((acc, curr) => acc + this.getNextValueInSequence(curr), 0);
    }
}
```

And that was enough to complete Part One.

## Part Two

Now we need to time travel, and work out what the _previous_ value in the sequence was.

This was as simple as taking my original `getNextValueInSequence`, and changing the `+` on the return line to `-`, against `seq[0]`:

```javascript
    public getPreviousValueInSequence(seq: number[]): number {
        if (new Set(seq).size === 1) return seq[0];
        const differences = seq.reduce((acc: number[], curr, i, arr) => {
            if (i === 0) return acc;
            acc.push(curr - arr[i - 1]);
            return acc;
        }, []);
        return seq[0] - this.getPreviousValueInSequence(differences);
    }

    public getTotalOfPreviousValuesInSequences(): number {
        return this.sequences.reduce((acc, curr) => acc + this.getPreviousValueInSequence(curr), 0);
    }
```

And that's Day Nine done! Much easier than yesterdays task.


