Skip to content

Commit

Permalink
feat: solved 2024 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 6, 2024
1 parent 22e0da0 commit ff5ff97
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2024.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2024",
"message": "3/25",
"message": "4/25",
"color": "orange"
}
2 changes: 1 addition & 1 deletion resources
9 changes: 7 additions & 2 deletions solutions/typescript/2024/04/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"type": "module",
"aoc": {
"day": 3,
"day": 4,
"year": 2024
},
"scripts": {
Expand All @@ -45,7 +45,7 @@
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
},
"exports": {
"./bench": {
Expand All @@ -64,6 +64,11 @@
"default": "./dist/p2.js"
},
"./package.json": "./package.json",
"./parse": {
"types": "./src/parse.ts",
"import": "./dist/parse.js",
"default": "./dist/parse.js"
},
"./readme": "./readme.md"
},
"dependencies": {
Expand Down
11 changes: 2 additions & 9 deletions solutions/typescript/2024/04/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 04 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(179834255);
expect(p1(resources.input)).toEqual(2427);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p1(resources.input)).toEqual(161);
expect(p1(resources.input)).toEqual(18);
});
});
});
26 changes: 21 additions & 5 deletions solutions/typescript/2024/04/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

export const p1 = (_input: string): number => {
return 0;
};
const searchWord = 'XMAS';

await task(p1, packageJson.aoc); // 0 ~0.09ms
export const p1 = (input: string): number =>
input
.toGridGraph({
connectionDirections: Direction.allDirections,
})
.nodeValues.filter((node) => node.toString() === searchWord[0])
.map((node) => {
return Direction.allDirections.filter((direction) => {
let walkResult = node.walkDirection(
direction,
(_next, distance) => distance < searchWord.length - 1,
);
const word = walkResult.nodes.map((n) => n.toString()).join('');
return word === searchWord;
}).length;
})
.sum();

await task(p1, packageJson.aoc); // 2427 ~91.00ms
11 changes: 2 additions & 9 deletions solutions/typescript/2024/04/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 04 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(80570939);
expect(p2(input)).toEqual(1900);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p2(input)).toEqual(48);
expect(p2(input)).toEqual(9);
});
});
});
38 changes: 34 additions & 4 deletions solutions/typescript/2024/04/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, GridGraphNode, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

export const p2 = (_input: string): number => {
return 0;
const isArmCorrect = (
nodeA: GridGraphNode | undefined,
nodeB: GridGraphNode | undefined,
): boolean => {
if (nodeA === undefined || nodeB === undefined) {
return false;
}

return (
(nodeA.toString() === 'S' && nodeB.toString() === 'M') ||
(nodeA.toString() === 'M' && nodeB.toString() === 'S')
);
};

export const p2 = (input: string): number => {
let g = input.toGridGraph({
connectionDirections: Direction.allDirections,
});
return g.nodeValues
.filter((node) => node.toString() === 'A')
.filter((node) => {
let arm1Correct = isArmCorrect(
node.getNeighbour(Direction.NORTHEAST),
node.getNeighbour(Direction.SOUTHWEST),
);
let arm2Correct = isArmCorrect(
node.getNeighbour(Direction.NORTHWEST),
node.getNeighbour(Direction.SOUTHEAST),
);

return arm1Correct && arm2Correct;
}).length;
};

await task(p2, packageJson.aoc); // 0 ~0.09ms
await task(p2, packageJson.aoc); // 1900 ~79.72ms
1 change: 0 additions & 1 deletion solutions/typescript/2024/04/src/parse.ts

This file was deleted.

4 changes: 2 additions & 2 deletions solutions/typescript/2024/05/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"type": "module",
"aoc": {
"day": 3,
"day": 5,
"year": 2024
},
"scripts": {
Expand All @@ -45,7 +45,7 @@
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
},
"exports": {
"./bench": {
Expand Down
11 changes: 2 additions & 9 deletions solutions/typescript/2024/05/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 05 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(179834255);
expect(p1(resources.input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p1(resources.input)).toEqual(161);
expect(p1(resources.input)).toEqual(0);
});
});
});
11 changes: 2 additions & 9 deletions solutions/typescript/2024/05/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 05 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(80570939);
expect(p2(input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p2(input)).toEqual(48);
expect(p2(input)).toEqual(0);
});
});
});
4 changes: 2 additions & 2 deletions solutions/typescript/2024/06/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"type": "module",
"aoc": {
"day": 3,
"day": 6,
"year": 2024
},
"scripts": {
Expand All @@ -45,7 +45,7 @@
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
},
"exports": {
"./bench": {
Expand Down
11 changes: 2 additions & 9 deletions solutions/typescript/2024/06/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 06 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(179834255);
expect(p1(resources.input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p1(resources.input)).toEqual(161);
expect(p1(resources.input)).toEqual(0);
});
});
});
11 changes: 2 additions & 9 deletions solutions/typescript/2024/06/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 06 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(80570939);
expect(p2(input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p2(input)).toEqual(48);
expect(p2(input)).toEqual(0);
});
});
});
4 changes: 2 additions & 2 deletions solutions/typescript/2024/07/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"type": "module",
"aoc": {
"day": 3,
"day": 7,
"year": 2024
},
"scripts": {
Expand All @@ -45,7 +45,7 @@
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
},
"exports": {
"./bench": {
Expand Down
11 changes: 2 additions & 9 deletions solutions/typescript/2024/07/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 07 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(179834255);
expect(p1(resources.input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p1(resources.input)).toEqual(161);
expect(p1(resources.input)).toEqual(0);
});
});
});
11 changes: 2 additions & 9 deletions solutions/typescript/2024/07/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ describe('2024 07 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(80570939);
expect(p2(input)).toEqual(0);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(161);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p2(input)).toEqual(48);
expect(p2(input)).toEqual(0);
});
});
});
11 changes: 8 additions & 3 deletions solutions/typescript/libs/lib/src/model/graph/grid-node.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ export class GridGraphNode<T extends ToString = string>
return this.walkDirection(Direction.NORTHWEST, until);
}

public walkDirection(direction: Direction, whileTrue?: (next: this) => boolean): WalkResult<T> {
const nodes: this[] = [];
public walkDirection(
direction: Direction,
whileTrue?: (next: this, distance: number) => boolean,
): WalkResult<T> {
const nodes: this[] = [this];
let neighbour = this.neighbours.get(direction)?.to;
let walkedToTheEnd = true;
let distance = 0;
while (neighbour) {
nodes.push(neighbour);
if (whileTrue && !whileTrue(neighbour)) {
distance++;
if (whileTrue && !whileTrue(neighbour, distance)) {
walkedToTheEnd = false;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions solutions/typescript/libs/lib/src/model/graph/node.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class GraphNode<T extends ToString, Dir extends ToString = Direction>
return this;
}

public getNeighbour(direction: Dir): this | undefined {
return this.neighbours.get(direction)?.to;
}

public directionTo(target: this): Dir | undefined {
return [...this.neighbours.entries()].find(
([, neightbour]) => neightbour.to === target,
Expand Down
Loading

0 comments on commit ff5ff97

Please sign in to comment.