Skip to content

Commit ff5ff97

Browse files
committed
feat: solved 2024 day 4
1 parent 22e0da0 commit ff5ff97

File tree

20 files changed

+99
-96
lines changed

20 files changed

+99
-96
lines changed

.github/badges/typescript/2024.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 1,
33
"label": "Advent of TypeScript 2024",
4-
"message": "3/25",
4+
"message": "4/25",
55
"color": "orange"
66
}

resources

solutions/typescript/2024/04/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"type": "module",
2121
"aoc": {
22-
"day": 3,
22+
"day": 4,
2323
"year": 2024
2424
},
2525
"scripts": {
@@ -45,7 +45,7 @@
4545
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
4646
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
4747
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
48-
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
48+
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
4949
},
5050
"exports": {
5151
"./bench": {
@@ -64,6 +64,11 @@
6464
"default": "./dist/p2.js"
6565
},
6666
"./package.json": "./package.json",
67+
"./parse": {
68+
"types": "./src/parse.ts",
69+
"import": "./dist/parse.js",
70+
"default": "./dist/parse.js"
71+
},
6772
"./readme": "./readme.md"
6873
},
6974
"dependencies": {

solutions/typescript/2024/04/src/p1.spec.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ describe('2024 04 p1', () => {
77
describe('the input', () => {
88
it('should solve the input', async () => {
99
const resources = await loadTaskResources(packageJson.aoc);
10-
expect(p1(resources.input)).toEqual(179834255);
10+
expect(p1(resources.input)).toEqual(2427);
1111
});
1212
});
1313

1414
describe('example 1', () => {
1515
it('should be solved', async () => {
1616
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
17-
expect(p1(resources.input)).toEqual(161);
18-
});
19-
});
20-
21-
describe('example 2', () => {
22-
it('should be solved', async () => {
23-
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
24-
expect(p1(resources.input)).toEqual(161);
17+
expect(p1(resources.input)).toEqual(18);
2518
});
2619
});
2720
});
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import { task } from '@alexaegis/advent-of-code-lib';
1+
import { Direction, task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
33

4-
export const p1 = (_input: string): number => {
5-
return 0;
6-
};
4+
const searchWord = 'XMAS';
75

8-
await task(p1, packageJson.aoc); // 0 ~0.09ms
6+
export const p1 = (input: string): number =>
7+
input
8+
.toGridGraph({
9+
connectionDirections: Direction.allDirections,
10+
})
11+
.nodeValues.filter((node) => node.toString() === searchWord[0])
12+
.map((node) => {
13+
return Direction.allDirections.filter((direction) => {
14+
let walkResult = node.walkDirection(
15+
direction,
16+
(_next, distance) => distance < searchWord.length - 1,
17+
);
18+
const word = walkResult.nodes.map((n) => n.toString()).join('');
19+
return word === searchWord;
20+
}).length;
21+
})
22+
.sum();
23+
24+
await task(p1, packageJson.aoc); // 2427 ~91.00ms

solutions/typescript/2024/04/src/p2.spec.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ describe('2024 04 p2', () => {
77
describe('the input', () => {
88
it('should solve the input', async () => {
99
const { input } = await loadTaskResources(packageJson.aoc);
10-
expect(p2(input)).toEqual(80570939);
10+
expect(p2(input)).toEqual(1900);
1111
});
1212
});
1313

1414
describe('example 1', () => {
1515
it('should be solved', async () => {
1616
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
17-
expect(p2(input)).toEqual(161);
18-
});
19-
});
20-
21-
describe('example 2', () => {
22-
it('should be solved', async () => {
23-
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
24-
expect(p2(input)).toEqual(48);
17+
expect(p2(input)).toEqual(9);
2518
});
2619
});
2720
});
Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
import { task } from '@alexaegis/advent-of-code-lib';
1+
import { Direction, GridGraphNode, task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
33

4-
export const p2 = (_input: string): number => {
5-
return 0;
4+
const isArmCorrect = (
5+
nodeA: GridGraphNode | undefined,
6+
nodeB: GridGraphNode | undefined,
7+
): boolean => {
8+
if (nodeA === undefined || nodeB === undefined) {
9+
return false;
10+
}
11+
12+
return (
13+
(nodeA.toString() === 'S' && nodeB.toString() === 'M') ||
14+
(nodeA.toString() === 'M' && nodeB.toString() === 'S')
15+
);
16+
};
17+
18+
export const p2 = (input: string): number => {
19+
let g = input.toGridGraph({
20+
connectionDirections: Direction.allDirections,
21+
});
22+
return g.nodeValues
23+
.filter((node) => node.toString() === 'A')
24+
.filter((node) => {
25+
let arm1Correct = isArmCorrect(
26+
node.getNeighbour(Direction.NORTHEAST),
27+
node.getNeighbour(Direction.SOUTHWEST),
28+
);
29+
let arm2Correct = isArmCorrect(
30+
node.getNeighbour(Direction.NORTHWEST),
31+
node.getNeighbour(Direction.SOUTHEAST),
32+
);
33+
34+
return arm1Correct && arm2Correct;
35+
}).length;
636
};
737

8-
await task(p2, packageJson.aoc); // 0 ~0.09ms
38+
await task(p2, packageJson.aoc); // 1900 ~79.72ms

solutions/typescript/2024/04/src/parse.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

solutions/typescript/2024/05/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"type": "module",
2121
"aoc": {
22-
"day": 3,
22+
"day": 5,
2323
"year": 2024
2424
},
2525
"scripts": {
@@ -45,7 +45,7 @@
4545
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
4646
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
4747
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts",
48-
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
48+
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p2.ts"
4949
},
5050
"exports": {
5151
"./bench": {

solutions/typescript/2024/05/src/p1.spec.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ describe('2024 05 p1', () => {
77
describe('the input', () => {
88
it('should solve the input', async () => {
99
const resources = await loadTaskResources(packageJson.aoc);
10-
expect(p1(resources.input)).toEqual(179834255);
10+
expect(p1(resources.input)).toEqual(0);
1111
});
1212
});
1313

1414
describe('example 1', () => {
1515
it('should be solved', async () => {
1616
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
17-
expect(p1(resources.input)).toEqual(161);
18-
});
19-
});
20-
21-
describe('example 2', () => {
22-
it('should be solved', async () => {
23-
const resources = await loadTaskResources(packageJson.aoc, 'example.2.txt');
24-
expect(p1(resources.input)).toEqual(161);
17+
expect(p1(resources.input)).toEqual(0);
2518
});
2619
});
2720
});

0 commit comments

Comments
 (0)