Skip to content

Commit 27bc441

Browse files
committed
feat: solved 2024 day 2
1 parent a5324b4 commit 27bc441

File tree

9 files changed

+61
-26
lines changed

9 files changed

+61
-26
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": "1/25",
4+
"message": "2/25",
55
"color": "orange"
66
}

resources

solutions/typescript/2024/02/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"format": "turbo run format_ --concurrency 16 --filter @alexaegis/advent-of-code-2024-02",
4444
"format_": "prettier --cache-location .cache/prettier --plugin prettier-plugin-svelte --plugin prettier-plugin-tailwindcss --write .",
4545
"p1": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts",
46-
"p2": "RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts"
46+
"p1:example": "RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts",
47+
"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"
4749
},
4850
"exports": {
4951
"./bench": {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ describe('2023 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(54_644);
10+
expect(p1(resources.input)).toEqual(299);
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(142);
17+
expect(p1(resources.input)).toEqual(2);
1818
});
1919
});
2020
});
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
3+
import { isReportSafe, parseReports } from './parse.js';
34

4-
export const p1 = (input: string): number => {
5-
return 0;
6-
};
7-
await task(p1, packageJson.aoc); // 0 ~0ms
5+
export const p1 = (input: string): number => parseReports(input).filter(isReportSafe).length;
6+
7+
await task(p1, packageJson.aoc); // 299 ~0.58ms

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ describe('2023 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(53_348);
10+
expect(p2(input)).toEqual(364);
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(142);
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(281);
17+
expect(p2(input)).toEqual(4);
2518
});
2619
});
2720
});
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { task } from '@alexaegis/advent-of-code-lib';
22
import packageJson from '../package.json' assert { type: 'json' };
3+
import { isReportSafe, parseReports } from './parse.js';
34

4-
export const p2 = (input: string): number => {
5-
return 0;
6-
};
7-
await task(p2, packageJson.aoc); // 0 ~0ms
5+
export const p2 = (input: string): number =>
6+
parseReports(input).filter((report) => {
7+
let safe = isReportSafe(report);
8+
for (let i = 0; i < report.length && !safe; i++) {
9+
const copyOfReport = [...report];
10+
copyOfReport.splice(i, 1);
11+
safe = isReportSafe(copyOfReport);
12+
}
13+
return safe;
14+
}).length;
15+
16+
await task(p2, packageJson.aoc); // 364 ~0.90ms
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1-
export interface Parsed {}
1+
export type Report = number[];
22

3-
export const parse = (input: string): Parsed => {
4-
return undefined;
3+
export const parseReports = (input: string): Report[] =>
4+
input.lines().map((line) => line.splitToInt());
5+
6+
export const isReportSafe = (report: Report): boolean => {
7+
let monotoneAscending = true;
8+
let monotoneDescending = true;
9+
let safeDiff = true;
10+
let last = undefined;
11+
for (const v of report) {
12+
if (last !== undefined && v <= last) {
13+
monotoneAscending = false;
14+
}
15+
16+
if (last !== undefined && v >= last) {
17+
monotoneDescending = false;
18+
}
19+
20+
if (last !== undefined) {
21+
const diff = Math.abs(last - v);
22+
if (diff < 1 || diff > 3) {
23+
safeDiff = false;
24+
break;
25+
}
26+
}
27+
28+
if (!monotoneAscending && !monotoneDescending) {
29+
break;
30+
}
31+
32+
last = v;
33+
}
34+
35+
return (monotoneAscending || monotoneDescending) && safeDiff;
536
};

solutions/typescript/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| Day | Part One | Part Two |
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) |
12-
| Day 2 | - | - |
12+
| [Day 2](/solutions/typescript/2024/02/) | [0.58ms](/solutions/typescript/2024/02/src/p1.ts) | [0.90ms](/solutions/typescript/2024/02/src/p2.ts) |
1313
| Day 3 | - | - |
1414
| Day 4 | - | - |
1515
| Day 5 | - | - |

0 commit comments

Comments
 (0)