Skip to content

Commit

Permalink
feat: solved 2024 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 2, 2024
1 parent a5324b4 commit 27bc441
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 26 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": "1/25",
"message": "2/25",
"color": "orange"
}
2 changes: 1 addition & 1 deletion resources
4 changes: 3 additions & 1 deletion solutions/typescript/2024/02/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"format": "turbo run format_ --concurrency 16 --filter @alexaegis/advent-of-code-2024-02",
"format_": "prettier --cache-location .cache/prettier --plugin prettier-plugin-svelte --plugin prettier-plugin-tailwindcss --write .",
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.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"
},
"exports": {
"./bench": {
Expand Down
4 changes: 2 additions & 2 deletions solutions/typescript/2024/02/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 01 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(54_644);
expect(p1(resources.input)).toEqual(299);
});
});

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

export const p1 = (input: string): number => {
return 0;
};
await task(p1, packageJson.aoc); // 0 ~0ms
export const p1 = (input: string): number => parseReports(input).filter(isReportSafe).length;

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

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(142);
});
});

describe('example 2', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.2.txt');
expect(p2(input)).toEqual(281);
expect(p2(input)).toEqual(4);
});
});
});
17 changes: 13 additions & 4 deletions solutions/typescript/2024/02/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };
import { isReportSafe, parseReports } from './parse.js';

export const p2 = (input: string): number => {
return 0;
};
await task(p2, packageJson.aoc); // 0 ~0ms
export const p2 = (input: string): number =>
parseReports(input).filter((report) => {
let safe = isReportSafe(report);
for (let i = 0; i < report.length && !safe; i++) {
const copyOfReport = [...report];
copyOfReport.splice(i, 1);
safe = isReportSafe(copyOfReport);
}
return safe;
}).length;

await task(p2, packageJson.aoc); // 364 ~0.90ms
37 changes: 34 additions & 3 deletions solutions/typescript/2024/02/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
export interface Parsed {}
export type Report = number[];

export const parse = (input: string): Parsed => {
return undefined;
export const parseReports = (input: string): Report[] =>
input.lines().map((line) => line.splitToInt());

export const isReportSafe = (report: Report): boolean => {
let monotoneAscending = true;
let monotoneDescending = true;
let safeDiff = true;
let last = undefined;
for (const v of report) {
if (last !== undefined && v <= last) {
monotoneAscending = false;
}

if (last !== undefined && v >= last) {
monotoneDescending = false;
}

if (last !== undefined) {
const diff = Math.abs(last - v);
if (diff < 1 || diff > 3) {
safeDiff = false;
break;
}
}

if (!monotoneAscending && !monotoneDescending) {
break;
}

last = v;
}

return (monotoneAscending || monotoneDescending) && safeDiff;
};
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Day | Part One | Part Two |
| --------------------------------------- | -------------------------------------------------- | -------------------------------------------------- |
| [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 | - | - |
| [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 4 | - | - |
| Day 5 | - | - |
Expand Down

0 comments on commit 27bc441

Please sign in to comment.