From ff5ff977645839fdeb31aa733ddc012295f99d4e Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Fri, 6 Dec 2024 22:46:59 +0100 Subject: [PATCH] feat: solved 2024 day 4 --- .github/badges/typescript/2024.json | 2 +- resources | 2 +- solutions/typescript/2024/04/package.json | 9 ++++- solutions/typescript/2024/04/src/p1.spec.ts | 11 +----- solutions/typescript/2024/04/src/p1.ts | 26 ++++++++++--- solutions/typescript/2024/04/src/p2.spec.ts | 11 +----- solutions/typescript/2024/04/src/p2.ts | 38 +++++++++++++++++-- solutions/typescript/2024/04/src/parse.ts | 1 - solutions/typescript/2024/05/package.json | 4 +- solutions/typescript/2024/05/src/p1.spec.ts | 11 +----- solutions/typescript/2024/05/src/p2.spec.ts | 11 +----- solutions/typescript/2024/06/package.json | 4 +- solutions/typescript/2024/06/src/p1.spec.ts | 11 +----- solutions/typescript/2024/06/src/p2.spec.ts | 11 +----- solutions/typescript/2024/07/package.json | 4 +- solutions/typescript/2024/07/src/p1.spec.ts | 11 +----- solutions/typescript/2024/07/src/p2.spec.ts | 11 +----- .../lib/src/model/graph/grid-node.class.ts | 11 ++++-- .../libs/lib/src/model/graph/node.class.ts | 4 ++ solutions/typescript/readme.md | 2 +- 20 files changed, 99 insertions(+), 96 deletions(-) delete mode 100644 solutions/typescript/2024/04/src/parse.ts diff --git a/.github/badges/typescript/2024.json b/.github/badges/typescript/2024.json index 241c645fa..797623ca0 100644 --- a/.github/badges/typescript/2024.json +++ b/.github/badges/typescript/2024.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, "label": "Advent of TypeScript 2024", - "message": "3/25", + "message": "4/25", "color": "orange" } diff --git a/resources b/resources index b630d0d7d..ab58d4cb8 160000 --- a/resources +++ b/resources @@ -1 +1 @@ -Subproject commit b630d0d7d9a747d2ed565b8c53eefe0ddcfb3347 +Subproject commit ab58d4cb85267e0258ba97c93526e7874b7b36a0 diff --git a/solutions/typescript/2024/04/package.json b/solutions/typescript/2024/04/package.json index 7afb51f20..18309fd5d 100644 --- a/solutions/typescript/2024/04/package.json +++ b/solutions/typescript/2024/04/package.json @@ -19,7 +19,7 @@ ], "type": "module", "aoc": { - "day": 3, + "day": 4, "year": 2024 }, "scripts": { @@ -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": { @@ -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": { diff --git a/solutions/typescript/2024/04/src/p1.spec.ts b/solutions/typescript/2024/04/src/p1.spec.ts index c00b8b05a..575b979ff 100644 --- a/solutions/typescript/2024/04/src/p1.spec.ts +++ b/solutions/typescript/2024/04/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/04/src/p1.ts b/solutions/typescript/2024/04/src/p1.ts index 89e41ee99..fc7b35843 100644 --- a/solutions/typescript/2024/04/src/p1.ts +++ b/solutions/typescript/2024/04/src/p1.ts @@ -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 diff --git a/solutions/typescript/2024/04/src/p2.spec.ts b/solutions/typescript/2024/04/src/p2.spec.ts index 7640bc5dc..8f81b6fd3 100644 --- a/solutions/typescript/2024/04/src/p2.spec.ts +++ b/solutions/typescript/2024/04/src/p2.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/04/src/p2.ts b/solutions/typescript/2024/04/src/p2.ts index d2aa81060..070659dc0 100644 --- a/solutions/typescript/2024/04/src/p2.ts +++ b/solutions/typescript/2024/04/src/p2.ts @@ -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 diff --git a/solutions/typescript/2024/04/src/parse.ts b/solutions/typescript/2024/04/src/parse.ts deleted file mode 100644 index b2f410d0a..000000000 --- a/solutions/typescript/2024/04/src/parse.ts +++ /dev/null @@ -1 +0,0 @@ -export const parse = (input: string): string[] => input.lines(); diff --git a/solutions/typescript/2024/05/package.json b/solutions/typescript/2024/05/package.json index 3a04e6675..2bab06dae 100644 --- a/solutions/typescript/2024/05/package.json +++ b/solutions/typescript/2024/05/package.json @@ -19,7 +19,7 @@ ], "type": "module", "aoc": { - "day": 3, + "day": 5, "year": 2024 }, "scripts": { @@ -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": { diff --git a/solutions/typescript/2024/05/src/p1.spec.ts b/solutions/typescript/2024/05/src/p1.spec.ts index d60af9535..af3a9ba63 100644 --- a/solutions/typescript/2024/05/src/p1.spec.ts +++ b/solutions/typescript/2024/05/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/05/src/p2.spec.ts b/solutions/typescript/2024/05/src/p2.spec.ts index 8dd6c5f61..b07d7ba53 100644 --- a/solutions/typescript/2024/05/src/p2.spec.ts +++ b/solutions/typescript/2024/05/src/p2.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/06/package.json b/solutions/typescript/2024/06/package.json index 8a464aaa4..7b601af75 100644 --- a/solutions/typescript/2024/06/package.json +++ b/solutions/typescript/2024/06/package.json @@ -19,7 +19,7 @@ ], "type": "module", "aoc": { - "day": 3, + "day": 6, "year": 2024 }, "scripts": { @@ -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": { diff --git a/solutions/typescript/2024/06/src/p1.spec.ts b/solutions/typescript/2024/06/src/p1.spec.ts index 2434ef986..6208e31ad 100644 --- a/solutions/typescript/2024/06/src/p1.spec.ts +++ b/solutions/typescript/2024/06/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/06/src/p2.spec.ts b/solutions/typescript/2024/06/src/p2.spec.ts index f8de88f39..9fdb811c3 100644 --- a/solutions/typescript/2024/06/src/p2.spec.ts +++ b/solutions/typescript/2024/06/src/p2.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/07/package.json b/solutions/typescript/2024/07/package.json index 28f1a6c63..2b7e058b9 100644 --- a/solutions/typescript/2024/07/package.json +++ b/solutions/typescript/2024/07/package.json @@ -19,7 +19,7 @@ ], "type": "module", "aoc": { - "day": 3, + "day": 7, "year": 2024 }, "scripts": { @@ -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": { diff --git a/solutions/typescript/2024/07/src/p1.spec.ts b/solutions/typescript/2024/07/src/p1.spec.ts index 6145294d9..eef1a7499 100644 --- a/solutions/typescript/2024/07/src/p1.spec.ts +++ b/solutions/typescript/2024/07/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/07/src/p2.spec.ts b/solutions/typescript/2024/07/src/p2.spec.ts index 21ff77b29..d77475ac6 100644 --- a/solutions/typescript/2024/07/src/p2.spec.ts +++ b/solutions/typescript/2024/07/src/p2.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/libs/lib/src/model/graph/grid-node.class.ts b/solutions/typescript/libs/lib/src/model/graph/grid-node.class.ts index eb961ebcc..b08fbe4b4 100644 --- a/solutions/typescript/libs/lib/src/model/graph/grid-node.class.ts +++ b/solutions/typescript/libs/lib/src/model/graph/grid-node.class.ts @@ -61,13 +61,18 @@ export class GridGraphNode return this.walkDirection(Direction.NORTHWEST, until); } - public walkDirection(direction: Direction, whileTrue?: (next: this) => boolean): WalkResult { - const nodes: this[] = []; + public walkDirection( + direction: Direction, + whileTrue?: (next: this, distance: number) => boolean, + ): WalkResult { + 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; } diff --git a/solutions/typescript/libs/lib/src/model/graph/node.class.ts b/solutions/typescript/libs/lib/src/model/graph/node.class.ts index 5f50a03ab..578044068 100644 --- a/solutions/typescript/libs/lib/src/model/graph/node.class.ts +++ b/solutions/typescript/libs/lib/src/model/graph/node.class.ts @@ -28,6 +28,10 @@ export class GraphNode 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, diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 2c3abd883..85b1b1bf8 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -11,7 +11,7 @@ | [Day 1](/solutions/typescript/2024/01/) | [16.04ms](/solutions/typescript/2024/01/src/p1.ts) | [18.95ms](/solutions/typescript/2024/01/src/p2.ts) | | [Day 2](/solutions/typescript/2024/02/) | [0.58ms](/solutions/typescript/2024/02/src/p1.ts) | [0.90ms](/solutions/typescript/2024/02/src/p2.ts) | | [Day 3](/solutions/typescript/2024/03/) | [0.09ms](/solutions/typescript/2024/03/src/p1.ts) | [0.11ms](/solutions/typescript/2024/03/src/p2.ts) | -| Day 4 | - | - | +| [Day 4](/solutions/typescript/2024/04/) | [91.00ms](/solutions/typescript/2024/04/src/p1.ts) | [79.72ms](/solutions/typescript/2024/04/src/p2.ts) | | Day 5 | - | - | | Day 6 | - | - | | Day 7 | - | - |