Skip to content

Commit 5d93156

Browse files
committed
feat: solved 2024 day 3
1 parent 45d3cd2 commit 5d93156

File tree

9 files changed

+96
-13
lines changed

9 files changed

+96
-13
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": "2/25",
4+
"message": "3/25",
55
"color": "orange"
66
}

resources

solutions/typescript/2024/03/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.1.txt tsx src/p2.ts"
48+
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
4949
},
5050
"exports": {
5151
"./bench": {

solutions/typescript/2024/03/src/bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { p1 } from './p1.js';
66
import { p2 } from './p2.js';
77

88
await defaultBench(
9-
'2024 - Day 2',
9+
'2024 - Day 3',
1010
add('Part One', async () => {
1111
const { input } = await loadTaskResources(packageJson.aoc);
1212
return () => p1(input);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ describe('2024 01 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(299);
10+
expect(p1(resources.input)).toEqual(179834255);
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(2);
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);
1825
});
1926
});
2027
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
33

4-
export const p1 = (input: string): number => {};
4+
const mulRegex = /mul\((\d+),(\d+)\)/g;
55

6-
await task(p1, packageJson.aoc); // 0 ~0ms
6+
export const p1 = (input: string): number => {
7+
const mulMatches = [...input.matchAll(mulRegex)];
8+
9+
return mulMatches.map((m) => parseInt(m[1] ?? '1', 10) * parseInt(m[2] ?? '1', 10)).sum();
10+
};
11+
12+
await task(p1, packageJson.aoc); // 179834255 ~0.09ms

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ describe('2024 01 p2', () => {
77
describe('the input', () => {
88
it('should solve the input', async () => {
99
const { input } = await loadTaskResources(packageJson.aoc);
10-
expect(p2(input)).toEqual(364);
10+
expect(p2(input)).toEqual(80570939);
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(4);
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);
1825
});
1926
});
2027
});
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
11
import { task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
33

4+
const instructionRegex = /(mul|don't|do)\((\d+)?,?(\d+)?\)/g;
5+
6+
interface DoInstruction {
7+
operator: Operator.DO;
8+
}
9+
10+
interface DontInstruction {
11+
operator: Operator.DONT;
12+
}
13+
14+
interface MulInstruction {
15+
operator: Operator.MUL;
16+
operandA: number;
17+
operandB: number;
18+
}
19+
20+
type Instruction = DoInstruction | DontInstruction | MulInstruction;
21+
22+
enum Operator {
23+
MUL = 'mul',
24+
DO = 'do',
25+
DONT = "don't",
26+
}
27+
28+
const parseInstruction = (match: RegExpExecArray): Instruction => {
29+
if (match[1] === Operator.MUL) {
30+
return {
31+
operator: match[1],
32+
operandA: parseInt(match[2] ?? '1', 10),
33+
operandB: parseInt(match[3] ?? '1', 10),
34+
} as MulInstruction;
35+
} else if (match[1] === Operator.DO) {
36+
return {
37+
operator: match[1],
38+
} as DoInstruction;
39+
} else if (match[1] === Operator.DONT) {
40+
return {
41+
operator: match[1],
42+
} as DontInstruction;
43+
} else {
44+
throw new Error('unsupported instruction: ' + match[0]);
45+
}
46+
};
47+
448
export const p2 = (input: string): number => {
5-
return 0;
49+
const instructions = [...input.matchAll(instructionRegex)].map(parseInstruction);
50+
51+
let enabled = true;
52+
let sum = 0;
53+
for (const i of instructions) {
54+
switch (i.operator) {
55+
case Operator.DO:
56+
enabled = true;
57+
break;
58+
case Operator.DONT:
59+
enabled = false;
60+
break;
61+
case Operator.MUL:
62+
if (enabled) {
63+
sum += i.operandA * i.operandB;
64+
}
65+
break;
66+
}
67+
}
68+
return sum;
669
};
770

8-
await task(p2, packageJson.aoc); // 364 ~0.90ms
71+
await task(p2, packageJson.aoc); // 80570939 ~0.11ms

solutions/typescript/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| --------------------------------------- | -------------------------------------------------- | -------------------------------------------------- |
1111
| [Day 1](/solutions/typescript/2024/01/) | [16.04ms](/solutions/typescript/2024/01/src/p1.ts) | [18.95ms](/solutions/typescript/2024/01/src/p2.ts) |
1212
| [Day 2](/solutions/typescript/2024/02/) | [0.58ms](/solutions/typescript/2024/02/src/p1.ts) | [0.90ms](/solutions/typescript/2024/02/src/p2.ts) |
13-
| Day 3 | - | - |
13+
| [Day 3](/solutions/typescript/2024/03/) | [0.09ms](/solutions/typescript/2024/03/src/p1.ts) | [0.11ms](/solutions/typescript/2024/03/src/p2.ts) |
1414
| Day 4 | - | - |
1515
| Day 5 | - | - |
1616
| Day 6 | - | - |

0 commit comments

Comments
 (0)