Skip to content

Commit

Permalink
feat: initial solution for 2023 day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 3, 2023
1 parent ef8642c commit dce1cae
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 13 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": "2/25",
"message": "3/25",
"color": "orange"
}
10 changes: 10 additions & 0 deletions resources/2023/03/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
140 changes: 140 additions & 0 deletions resources/2023/03/input.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion solutions/typescript/2023/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(
'2023 - Day 2',
'2023 - Day 3',
add('Part One', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
return () => p1(input);
Expand Down
4 changes: 2 additions & 2 deletions solutions/typescript/2023/03/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 03 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(539_637);
});
});

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(4361);
});
});
});
61 changes: 58 additions & 3 deletions solutions/typescript/2023/03/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
import { task } from '@alexaegis/advent-of-code-lib';
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/prefer-for-of */
import { Direction, Vec2, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';

export const p1 = (input: string): number => {
return input.lines().length;
const m = input.toMatrix();
const numbers: number[] = [];
let num: string | undefined = undefined;
let lastVecs: Vec2[] = [];

for (let y = 0; y < m.length; y++) {
const row = m[y]!;

for (let x = 0; x < row.length; x++) {
const v = row[x]!;

if (/\d/.test(v)) {
num = num === undefined ? v : num + v;
lastVecs.push(new Vec2({ x, y: y }));
} else if (
num !== undefined &&
lastVecs.some((lv) =>
Direction.allDirections
.map((d) => d.add(lv))
.some((v) => {
// const cv = new Vec2(x, i);
const n = m[v.y]?.[v.x];
if (n) {
// console.log(num, n, v.toString());
}
return (
n &&
(n === '#' ||
n === '$' ||
n === '%' ||
n === '&' ||
n === '*' ||
n === '+' ||
n === '/' ||
n === '=' ||
n === '-' ||
n === '@')
);
}),
)
) {
//TODO check surr
numbers.push(num.toInt());
// console.log('FOUND', num);
num = undefined;
lastVecs = [];
} else {
num = undefined;
lastVecs = [];
}
}
}

return numbers.sum();
};

await task(p1, packageJson.aoc); // 2149 ~0.22ms
await task(p1, packageJson.aoc); // 539637 ~1.39ms
4 changes: 2 additions & 2 deletions solutions/typescript/2023/03/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 03 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(82_818_007);
});
});

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(467_835);
});
});
});
58 changes: 55 additions & 3 deletions solutions/typescript/2023/03/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
import { task } from '@alexaegis/advent-of-code-lib';
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/prefer-for-of */
import { Direction, Vec2, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';

export const p2 = (input: string): number => {
return input.lines().length;
const m = input.toMatrix();
const numbers: number[] = [];
let num: string | undefined = undefined;
let lastVecs: Vec2[] = [];

const gears = new Map<string, number[]>();
for (let y = 0; y < m.length; y++) {
const row = m[y]!;

for (let x = 0; x < row.length; x++) {
const v = row[x]!;

const gearVec = lastVecs
.map((lv) =>
Direction.allDirections
.map((d) => d.add(lv))
.find((v) => {
// const cv = new Vec2(x, i);
const n = m[v.y]?.[v.x];
if (n) {
// console.log(num, n, v.toString());
}
return n && n === '*';
}),
)
.find((v) => !!v);

if (/\d/.test(v)) {
num = num === undefined ? v : num + v;
lastVecs.push(new Vec2({ x, y: y }));
} else if (num !== undefined && gearVec) {
//TODO check surr
numbers.push(num.toInt());
if (gears.has(gearVec.toString())) {
gears.get(gearVec.toString())?.push(num.toInt());
} else {
gears.set(gearVec.toString(), [num.toInt()]);
}
num = undefined;
lastVecs = [];
} else {
num = undefined;
lastVecs = [];
}
}
}

return [...gears.values()]
.filter((gp) => gp.length === 2)
.map((gp) => gp.product())
.sum();
};

await task(p2, packageJson.aoc); // 0 ~0.22ms
await task(p2, packageJson.aoc); // 82818007 ~3.89ms
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/2023/01/) | [0.19ms](/solutions/typescript/2023/01/src/p1.ts) | [1.38ms](/solutions/typescript/2023/01/src/p2.ts) |
| [Day 2](/solutions/typescript/2023/02/) | [0.22ms](/solutions/typescript/2023/02/src/p1.ts) | [0.25ms](/solutions/typescript/2023/02/src/p2.ts) |
| Day 3 | - | - |
| [Day 3](/solutions/typescript/2023/03/) | [1.39ms](/solutions/typescript/2023/03/src/p1.ts) | [3.89ms](/solutions/typescript/2023/03/src/p2.ts) |
| Day 4 | - | - |
| Day 5 | - | - |
| Day 6 | - | - |
Expand Down

0 comments on commit dce1cae

Please sign in to comment.