Skip to content

Commit

Permalink
feat: solved 2023 day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 15, 2023
1 parent f608c51 commit 635f029
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2023.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2023",
"message": "12/25",
"message": "13/25",
"color": "orange"
}
1 change: 1 addition & 0 deletions resources/2023/15/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
1 change: 1 addition & 0 deletions resources/2023/15/input.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions solutions/typescript/2023/15/src/internal/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, expect, it } from 'vitest';
import { hash } from './hash.js';

describe('hash', () => {
it('should, return for the string hash', () => {
expect(hash('HASH')).toEqual(52);
});
});
9 changes: 9 additions & 0 deletions solutions/typescript/2023/15/src/internal/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const hash = (s: string): number => {
let value = 0;
for (let i = 0; i < s.length; i++) {
value += s.codePointAt(i) ?? 0;
value *= 17;
value %= 256;
}
return value;
};
42 changes: 42 additions & 0 deletions solutions/typescript/2023/15/src/internal/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { hash } from './hash.js';

interface SetInstruction {
label: string;
box: number;
operator: '=';
focalLength: number;
}

interface RemoveInstruction {
label: string;
box: number;
operator: '-';
}

type Instruction = SetInstruction | RemoveInstruction;

const instructionRegex = /(.*)([=-])(.*)/;

export const parseInstruction = (instruction: string): Instruction => {
const match = instructionRegex.exec(instruction);
if (!match) {
throw new Error('instruction not matching regex: ' + instruction);
}
const [, label, operator, value] = match;
if (operator === '=' && value !== undefined && label !== undefined) {
return {
focalLength: Number.parseInt(value, 10),
label,
operator,
box: hash(label),
};
} else if (operator === '-' && label !== undefined) {
return {
label,
operator,
box: hash(label),
};
} else {
throw new Error('invalid instruction: ' + instruction);
}
};
4 changes: 2 additions & 2 deletions solutions/typescript/2023/15/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 15 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(495_972);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(1320);
});
});
});
7 changes: 3 additions & 4 deletions solutions/typescript/2023/15/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { hash } from './internal/hash.js';

export const p1 = (_input: string): number => {
return 0;
};
export const p1 = (input: string): number => input.lines(false).first().split(',').map(hash).sum();

await task(p1, packageJson.aoc); // 0 ~0ms
await task(p1, packageJson.aoc); // 495972 ~0.19ms
4 changes: 2 additions & 2 deletions solutions/typescript/2023/15/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 15 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(245_223);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(145);
});
});
});
40 changes: 37 additions & 3 deletions solutions/typescript/2023/15/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parseInstruction } from './internal/parse.js';

export const p2 = (_input: string): number => {
return 0;
interface Lens {
label: string;
focalLength: number;
}

export const p2 = (input: string): number => {
const sequence = input.lines(false).first().split(',').map(parseInstruction);
const boxMap = new Map<number, Lens[]>();

for (const instruction of sequence) {
if (instruction.operator === '-') {
const lenses = boxMap.get(instruction.box);
const lenseIndex = lenses?.findIndex((lens) => lens.label === instruction.label) ?? -1;
if (lenses !== undefined && lenseIndex >= 0) {
lenses.splice(lenseIndex, 1);
if (lenses.length === 0) {
boxMap.delete(instruction.box);
}
}
} else {
const lenses = boxMap.getOrAdd(instruction.box, (_key) => []);
const existingLens = lenses.find((lens) => lens.label === instruction.label);
if (existingLens) {
existingLens.focalLength = instruction.focalLength;
} else {
lenses.push({ focalLength: instruction.focalLength, label: instruction.label });
}
}
}

return [...boxMap.entries()]
.flatMap(([box, lenses]) =>
lenses.map((lens, lenseIndex) => (box + 1) * (lenseIndex + 1) * lens.focalLength),
)
.sum();
};

await task(p2, packageJson.aoc); // 0 ~0ms
await task(p2, packageJson.aoc); // 245223 ~0.65ms
6 changes: 3 additions & 3 deletions solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
| [Day 10](/solutions/typescript/2023/10/) | [68.44ms](/solutions/typescript/2023/10/src/p1.ts) | [1.4s](/solutions/typescript/2023/10/src/p2.ts) |
| [Day 11](/solutions/typescript/2023/11/) | [104.54ms](/solutions/typescript/2023/11/src/p1.ts) | [104.48ms](/solutions/typescript/2023/11/src/p2.ts) |
| [Day 12](/solutions/typescript/2023/12/) | [30.83ms](/solutions/typescript/2023/12/src/p1.ts) | [539.06ms](/solutions/typescript/2023/12/src/p2.ts) |
| Day 13 | - | - |
| Day 14 | - | - |
| Day 15 | - | - |
| [Day 13](/solutions/typescript/2023/13/) | [?ms](/solutions/typescript/2023/13/src/p1.ts) | [?ms](/solutions/typescript/2023/13/src/p2.ts) |
| [Day 14](/solutions/typescript/2023/14/) | [?ms](/solutions/typescript/2023/14/src/p1.ts) | [?ms](/solutions/typescript/2023/14/src/p2.ts) |
| [Day 15](/solutions/typescript/2023/15/) | [0.19ms](/solutions/typescript/2023/15/src/p1.ts) | [0.65ms](/solutions/typescript/2023/15/src/p2.ts) |
| Day 16 | - | - |
| Day 17 | - | - |
| Day 18 | - | - |
Expand Down

0 comments on commit 635f029

Please sign in to comment.