Skip to content

Commit

Permalink
feat: solved 2024 day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 8, 2024
1 parent ff5ff97 commit 630d133
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 14 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": "4/25",
"message": "5/25",
"color": "orange"
}
2 changes: 1 addition & 1 deletion resources
5 changes: 5 additions & 0 deletions solutions/typescript/2024/05/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions solutions/typescript/2024/05/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +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(0);
expect(p1(resources.input)).toEqual(4569);
});
});

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(143);
});
});
});
15 changes: 12 additions & 3 deletions solutions/typescript/2024/05/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };
import { createUpdateComparator, parse } from './parse.js';

export const p1 = (_input: string): number => {
return 0;
export const p1 = (input: string): number => {
const p = parse(input);
const comparator = createUpdateComparator(p.rules);
return p.updates
.filter((update) => {
const sortedUpdate = [...update.pages].sort(comparator);
return sortedUpdate.join(',') === update.pages.join(',');
})
.map((update) => update.pages[Math.floor(update.pages.length / 2)])
.sum();
};

await task(p1, packageJson.aoc); // 0 ~0.09ms
await task(p1, packageJson.aoc); // 4569 ~24.54ms
4 changes: 2 additions & 2 deletions solutions/typescript/2024/05/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +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(0);
expect(p2(input)).toEqual(6456);
});
});

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(123);
});
});
});
15 changes: 12 additions & 3 deletions solutions/typescript/2024/05/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };
import { createUpdateComparator, parse } from './parse.js';

export const p2 = (_input: string): number => {
return 0;
export const p2 = (input: string): number => {
const p = parse(input);
const comparator = createUpdateComparator(p.rules);
return p.updates
.filterMap((update) => {
const sortedUpdate = [...update.pages].sort(comparator);
return sortedUpdate.join(',') !== update.pages.join(',') ? sortedUpdate : undefined;
})
.map((pages) => pages[Math.floor(pages.length / 2)])
.sum();
};

await task(p2, packageJson.aoc); // 0 ~0.09ms
await task(p2, packageJson.aoc); // 6456 ~24.37ms
52 changes: 51 additions & 1 deletion solutions/typescript/2024/05/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
export const parse = (input: string): string[] => input.lines();
import { DOUBLE_NEWLINE } from '@alexaegis/advent-of-code-lib';

export interface Rule {
before: number;
after: number;
}

export interface Update {
pages: number[];
}

export interface Parsed {
rules: Rule[];
updates: Update[];
}

export const createUpdateComparator = (rules: Rule[]) => (a: number, b: number) => {
const mustBeBefore = rules.filter((rule) => rule.before === a).some((rule) => rule.after === b);
const mustBeAfter = rules.filter((rule) => rule.after === a).some((rule) => rule.before === b);

if (mustBeBefore && mustBeAfter) {
return 0;
} else if (mustBeBefore) {
return -1;
} else {
return 1;
}
};

export const parse = (input: string): Parsed => {
let [rulesText, pagesText] = input.split(DOUBLE_NEWLINE);

if (rulesText === undefined || pagesText === undefined) {
throw new Error('Invalid input');
}

const rules = rulesText.lines().map((rawRule) => {
const [before, after] = rawRule.splitToInt({
delimiter: /\|/,
});
if (before === undefined || after === undefined) {
throw new Error('Invalid number');
}
return { before, after };
});
const updates = pagesText
.lines()
.map((rawRule) => ({ pages: rawRule.splitToInt({ delimiter: /,/ }) }) as Update);

return { rules, updates };
};
9 changes: 9 additions & 0 deletions solutions/typescript/libs/lib/src/array/array.polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare global {
contains(item: T): boolean;
intoSet(set?: Set<T>): Set<T>;
tap(callbackFn: (item: T, index: number) => void): T[];
log(prefix?: string): T[];
toInt(
options?:
| { radix?: number; safe?: boolean; keepNonNumbers: false }
Expand Down Expand Up @@ -213,6 +214,14 @@ Array.prototype.tap = function <T>(this: T[], callbackFn: (item: T, index: numbe
return this;
};

Array.prototype.log = function <T>(this: T[], prefix?: string): T[] {
for (let i = 0; i < this.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
console.log(prefix ?? 'log:', this[i]!, i);
}
return this;
};

Array.prototype.mean = function (): number {
if (!isNumberArray(this)) {
throw new Error('Mean can only be calculated for number[]');
Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| [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](/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 5](/solutions/typescript/2024/05/) | [24.54ms](/solutions/typescript/2024/05/src/p1.ts) | [24.37ms](/solutions/typescript/2024/05/src/p2.ts) |
| Day 6 | - | - |
| Day 7 | - | - |
| Day 8 | - | - |
Expand Down

0 comments on commit 630d133

Please sign in to comment.