Skip to content

Commit

Permalink
feat: solved 2023 day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 9, 2023
1 parent 9ae8219 commit 14115b2
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2023.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2023",
"message": "7/25",
"message": "8/25",
"color": "orange"
}
3 changes: 3 additions & 0 deletions resources/2023/09/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
200 changes: 200 additions & 0 deletions resources/2023/09/input.txt

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions solutions/typescript/2023/08/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"import": "./dist/p2.js",
"default": "./dist/p2.js"
},
"./parse": {
"types": "./src/parse.ts",
"import": "./dist/parse.js",
"default": "./dist/parse.js"
},
"./readme": "./readme.md"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions solutions/typescript/2023/09/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 09 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(1_938_731_307);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(114);
});
});
});
19 changes: 16 additions & 3 deletions solutions/typescript/2023/09/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { extrapolateSequence, parse } from './parse.js';

export const p1 = (_input: string): number => {
return 0;
const extrapolateNextInStack = (sequenceStack: number[][]): number[][] => {
let carry = sequenceStack.last().last();
for (let i = sequenceStack.length - 1; i >= 0; i--) {
carry = (sequenceStack[i]?.last() ?? 0) + carry;
sequenceStack[i]?.push(carry);
}
return sequenceStack;
};

await task(p1, packageJson.aoc); // 0 ~0ms
export const p1 = (input: string): number =>
parse(input)
.map(extrapolateSequence)
.map(extrapolateNextInStack)
.map((extrapolatedStack) => extrapolatedStack.first().last())
.sum();

await task(p1, packageJson.aoc); // 1938731307 ~1.57ms
4 changes: 2 additions & 2 deletions solutions/typescript/2023/09/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 09 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(948);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(2);
});
});
});
19 changes: 16 additions & 3 deletions solutions/typescript/2023/09/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { extrapolateSequence, parse } from './parse.js';

export const p2 = (_input: string): number => {
return 0;
const extrapolatePreviousInStack = (sequenceStack: number[][]): number[][] => {
let carry = sequenceStack.last().first();
for (let i = sequenceStack.length - 1; i >= 0; i--) {
carry = (sequenceStack[i]?.first() ?? 0) - carry;
sequenceStack[i]?.unshift(carry);
}
return sequenceStack;
};

await task(p2, packageJson.aoc); // 0 ~0ms
export const p2 = (input: string): number =>
parse(input)
.map(extrapolateSequence)
.map(extrapolatePreviousInStack)
.map((extrapolatedStack) => extrapolatedStack.first().first())
.sum();

await task(p2, packageJson.aoc); // 948 ~1.62ms
15 changes: 15 additions & 0 deletions solutions/typescript/2023/09/src/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const parse = (input: string): number[][] =>
input.lines(false).map((line) => line.splitToInt());

export const extrapolateSequence = (sequence: number[]): number[][] => {
const sequenceStack: number[][] = [sequence];
while (sequenceStack.last().some((i) => i !== 0)) {
sequenceStack.push(
sequenceStack
.last()
.slideWindow(2, 1)
.map(([left, right]) => right - left),
);
}
return sequenceStack;
};
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| [Day 6](/solutions/typescript/2023/06/) | [40μs](/solutions/typescript/2023/06/src/p1.ts) | [7μs](/solutions/typescript/2023/06/src/p2.ts) |
| [Day 7](/solutions/typescript/2023/07/) | [8.70ms](/solutions/typescript/2023/07/src/p1.ts) | [20.46ms](/solutions/typescript/2023/07/src/p2.ts) |
| [Day 8](/solutions/typescript/2023/08/) | [10.25ms](/solutions/typescript/2023/08/src/p1.ts) | [11.44ms](/solutions/typescript/2023/08/src/p2.ts) |
| Day 9 | - | - |
| [Day 9](/solutions/typescript/2023/09/) | [1.57ms](/solutions/typescript/2023/09/src/p1.ts) | [1.62ms](/solutions/typescript/2023/09/src/p2.ts) |
| Day 10 | - | - |
| Day 11 | - | - |
| Day 12 | - | - |
Expand Down

0 comments on commit 14115b2

Please sign in to comment.