Skip to content

Commit

Permalink
feat: solved 2024 day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 3, 2024
1 parent 45d3cd2 commit 5d93156
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 13 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": "2/25",
"message": "3/25",
"color": "orange"
}
2 changes: 1 addition & 1 deletion resources
2 changes: 1 addition & 1 deletion solutions/typescript/2024/03/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.txt tsx src/p2.ts"
"p2:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2.txt tsx src/p2.ts"
},
"exports": {
"./bench": {
Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/2024/03/src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { p1 } from './p1.js';
import { p2 } from './p2.js';

await defaultBench(
'2024 - Day 2',
'2024 - Day 3',
add('Part One', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
return () => p1(input);
Expand Down
11 changes: 9 additions & 2 deletions solutions/typescript/2024/03/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ describe('2024 01 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(299);
expect(p1(resources.input)).toEqual(179834255);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(2);
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);
});
});
});
10 changes: 8 additions & 2 deletions solutions/typescript/2024/03/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

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

await task(p1, packageJson.aoc); // 0 ~0ms
export const p1 = (input: string): number => {
const mulMatches = [...input.matchAll(mulRegex)];

return mulMatches.map((m) => parseInt(m[1] ?? '1', 10) * parseInt(m[2] ?? '1', 10)).sum();
};

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

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(4);
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);
});
});
});
67 changes: 65 additions & 2 deletions solutions/typescript/2024/03/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

const instructionRegex = /(mul|don't|do)\((\d+)?,?(\d+)?\)/g;

interface DoInstruction {
operator: Operator.DO;
}

interface DontInstruction {
operator: Operator.DONT;
}

interface MulInstruction {
operator: Operator.MUL;
operandA: number;
operandB: number;
}

type Instruction = DoInstruction | DontInstruction | MulInstruction;

enum Operator {
MUL = 'mul',
DO = 'do',
DONT = "don't",
}

const parseInstruction = (match: RegExpExecArray): Instruction => {
if (match[1] === Operator.MUL) {
return {
operator: match[1],
operandA: parseInt(match[2] ?? '1', 10),
operandB: parseInt(match[3] ?? '1', 10),
} as MulInstruction;
} else if (match[1] === Operator.DO) {
return {
operator: match[1],
} as DoInstruction;
} else if (match[1] === Operator.DONT) {
return {
operator: match[1],
} as DontInstruction;
} else {
throw new Error('unsupported instruction: ' + match[0]);
}
};

export const p2 = (input: string): number => {
return 0;
const instructions = [...input.matchAll(instructionRegex)].map(parseInstruction);

let enabled = true;
let sum = 0;
for (const i of instructions) {
switch (i.operator) {
case Operator.DO:
enabled = true;
break;
case Operator.DONT:
enabled = false;
break;
case Operator.MUL:
if (enabled) {
sum += i.operandA * i.operandB;
}
break;
}
}
return sum;
};

await task(p2, packageJson.aoc); // 364 ~0.90ms
await task(p2, packageJson.aoc); // 80570939 ~0.11ms
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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 | - | - |
| [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 5 | - | - |
| Day 6 | - | - |
Expand Down

0 comments on commit 5d93156

Please sign in to comment.