Skip to content

Commit

Permalink
feat: solved 2023 day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 11, 2023
1 parent 449f8ce commit f30c5ad
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 36 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": "10/25",
"message": "11/25",
"color": "orange"
}
10 changes: 10 additions & 0 deletions resources/2023/11/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
140 changes: 140 additions & 0 deletions resources/2023/11/input.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion solutions/typescript/2021/15/src/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const p1 = (input: string): number => {
const start = graph.getNode(boundingBox.topLeft);
const end = graph.getNode(boundingBox.bottomRight);

const path = graph.aStar(start, end, {
const { path } = graph.aStar(start, end, {
heuristic: (_currentNode, tentativePath) => tentativePath.map((n) => n.value).sum(),
});

Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/2021/15/src/p2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const p2 = (input: string): number => {
const start = graph.getNode(boundingBox.topLeft);
const end = graph.getNode(boundingBox.bottomRight);

const path = graph.aStar(start, end, {
const { path } = graph.aStar(start, end, {
heuristic: (_a, p) => p.map((n) => n.value).sum(),
});

Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/2022/12/src/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const p1 = (input: string): number => {
throw new Error('No path found');
}

return graph.aStar(start, end).length - 1;
return graph.aStar(start, end).path.length - 1;
};

await task(p1, packageJson.aoc); // 534 ~83.84ms
2 changes: 1 addition & 1 deletion solutions/typescript/2022/12/src/p2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const p2 = (input: string): number => {

const start = graph.findNode((n) => n.value === 'E');

return graph.aStar(start, (n) => n.value === 'a').length - 1;
return graph.aStar(start, (n) => n.value === 'a').path.length - 1;
};

await task(p2, packageJson.aoc); // 525 ~140.04ms
2 changes: 1 addition & 1 deletion solutions/typescript/2022/16/src/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pathBetweenValves = memoize(
): GraphNode<Valve, string>[] => {
const from = graph.getNode(fromValve);
const to = graph.getNode(toValve);
return graph.aStar(from, to);
return graph.aStar(from, to).path;
},
);

Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/2022/16/src/p2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pathBetweenValves = memoize(
): GraphNode<Valve, string>[] => {
const from = graph.getNode(fromValve);
const to = graph.getNode(toValve);
return graph.aStar(from, to);
return graph.aStar(from, to).path;
},
);

Expand Down
5 changes: 0 additions & 5 deletions solutions/typescript/2023/11/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@
"import": "./dist/p2.js",
"default": "./dist/p2.js"
},
"./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/2023/11/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 11 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(10_228_230);
});
});

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(374);
});
});
});
23 changes: 19 additions & 4 deletions solutions/typescript/2023/11/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { BoundingBox, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parse } from './parse.js';

export const p1 = (_input: string): number => {
return 0;
export const p1 = (input: string): number => {
const { galaxies, emptyRows, emptyColumns } = parse(input);

return galaxies
.pairs()
.map(([a, b]) => {
const nonExpandedDistance = a.manhattan(b);
const galaxyBox = BoundingBox.fromVectors([a, b]);
const emptyRowCount = emptyRows.filter((i) => galaxyBox.vertical.contains(i)).length;
const emptyColumnCount = emptyColumns.filter((i) =>
galaxyBox.horizontal.contains(i),
).length;

return nonExpandedDistance + emptyRowCount + emptyColumnCount;
})
.sum();
};

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

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(82_000_210);
});
});
});
23 changes: 19 additions & 4 deletions solutions/typescript/2023/11/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { BoundingBox, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parse } from './parse.js';

export const p2 = (_input: string): number => {
return 0;
export const p2 = (input: string): number => {
const { galaxies, emptyRows, emptyColumns } = parse(input);

return galaxies
.pairs()
.map(([a, b]) => {
const nonExpandedDistance = a.manhattan(b);
const galaxyBox = BoundingBox.fromVectors([a, b]);
const emptyRowCount = emptyRows.filter((i) => galaxyBox.vertical.contains(i)).length;
const emptyColumnCount = emptyColumns.filter((i) =>
galaxyBox.horizontal.contains(i),
).length;

return nonExpandedDistance + emptyRowCount * 999_999 + emptyColumnCount * 999_999;
})
.sum();
};

await task(p2, packageJson.aoc); // 0 ~0ms
await task(p2, packageJson.aoc); // 10228230 ~0ms
37 changes: 37 additions & 0 deletions solutions/typescript/2023/11/src/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Vec2 } from '@alexaegis/advent-of-code-lib';

export interface GalaxyMap {
galaxies: Vec2[];
emptyRows: number[];
emptyColumns: number[];
}

export const parse = (input: string): GalaxyMap => {
const gg = input.toGridGraph({
weighter: (_a, _b) => 1,
});

const verticalIndices = gg.boundingBox().vertical.collectValues();
const horizontalIndices = gg.boundingBox().horizontal.collectValues();

const emptyRows = verticalIndices
.map((y) => horizontalIndices.map((x) => new Vec2(x, y)))
.filterMap((column, i) => {
const colNodes = column.map((c) => gg.getNode(c));
return colNodes.every((node) => node?.value === '.') ? i : undefined;
});

const emptyColumns = horizontalIndices
.map((x) => verticalIndices.map((y) => new Vec2(x, y)))
.filterMap((row, i) => {
const rowNodes = row.map((r) => gg.getNode(r));
return rowNodes.every((node) => node?.value === '.') ? i : undefined;
});

const galaxies = gg.nodes
.valueArray()
.filter((node) => node.value !== '.')
.map((node) => node.coordinate);

return { galaxies, emptyRows, emptyColumns };
};
4 changes: 2 additions & 2 deletions solutions/typescript/libs/lib/src/array/array.polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ declare global {
* Return the average value of the array
*/
mean(): number;
filterMap<V>(mapFn: (t: T) => V | undefined): V[];
filterMap<V>(mapFn: (t: T, i: number) => V | undefined): V[];
partition(partitioner: (a: T) => boolean): [T[], T[]];
pairwise(callback: (a: T, b: T) => void): void;
slideWindow<N extends number = 2>(windowSize?: N, stepSize?: number): SizedTuple<T, N>[];
Expand Down Expand Up @@ -187,7 +187,7 @@ Array.prototype.unique = function <T>(this: T[], comparator?: (a: T, b: T) => bo
return result;
};

Array.prototype.filterMap = function <T, V>(mapFn: (t: T) => V | undefined): V[] {
Array.prototype.filterMap = function <T, V>(mapFn: (t: T, i: number) => V | undefined): V[] {
return filterMap(this, mapFn);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { nonNullish } from '../functions/non-nullish.function.js';

export const filterMap = <T, V>(array: Iterable<T>, mapFn: (t: T) => V | undefined): V[] => {
export const filterMap = <T, V>(
array: Iterable<T>,
mapFn: (t: T, i: number) => V | undefined,
): V[] => {
const result: V[] = [];
let i = 0;
for (const item of array) {
const value = mapFn(item);
const value = mapFn(item, i);
if (nonNullish(value)) {
result.push(value);
}
i++;
}
return result;
};
6 changes: 3 additions & 3 deletions solutions/typescript/libs/lib/src/model/graph/graph.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ export class Graph<
start: N | undefined,
end: N | ((n: N, path: N[]) => boolean) | undefined,
options?: GraphTraversalOptions<N, Dir>,
): N[] {
): { path: N[]; gScore: Map<N, number> } {
if (!start || !end) {
return [];
return { path: [], gScore: new Map() };
}

const openSet = new Set<N>([start]); // q?
Expand Down Expand Up @@ -351,6 +351,6 @@ export class Graph<
}
}

return Graph.generatePath(cameFrom, start, goal);
return { path: Graph.generatePath(cameFrom, start, goal), gScore };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const graph = GridGraph.fromString(mazeStr, {
graph.print();
const start = graph.getNode(new Vec2(1, 1))!;
const goal = graph.getNode(new Vec2(11, 12))!;
const p = graph.aStar(start, goal, {
const { path } = graph.aStar(start, goal, {
weighter: (a, b) => a.coordinate.dist(b.coordinate),
});
console.log(
graph.toString((node) =>
p.some((pc) => node.coordinate.equals(pc.coordinate)) ? '#' : undefined,
path.some((pc) => node.coordinate.equals(pc.coordinate)) ? '#' : undefined,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Grid Graph', () => {
it('should find the shortest path', () => {
const g = GridGraph.fromMatrix(matrix);
const goal = g.getNode(finish)!;
const path = g.aStar(g.getNode(start), goal, {
const { path } = g.aStar(g.getNode(start), goal, {
heuristic: (a) => a.coordinate.manhattan(goal.coordinate),
});
expect(path.length).toEqual(10);
Expand All @@ -51,7 +51,7 @@ describe('Grid Graph', () => {
connectionDirections: Direction.allDirections,
});
const goal = g.getNode(finish)!;
const path = g.aStar(g.getNode(start), goal, {
const { path } = g.aStar(g.getNode(start), goal, {
heuristic: (a) => a.coordinate.manhattan(goal.coordinate),
});
expect(path.length).toEqual(6);
Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| [Day 8](/solutions/typescript/2023/08/) | [10.25ms](/solutions/typescript/2023/08/src/p1.ts) | [11.44ms](/solutions/typescript/2023/08/src/p2.ts) |
| [Day 9](/solutions/typescript/2023/09/) | [1.57ms](/solutions/typescript/2023/09/src/p1.ts) | [1.62ms](/solutions/typescript/2023/09/src/p2.ts) |
| [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 | - | - |
| [Day 11](/solutions/typescript/2023/11/) | [0ms](/solutions/typescript/2023/11/src/p1.ts) | [0ms](/solutions/typescript/2023/11/src/p2.ts) |
| Day 12 | - | - |
| Day 13 | - | - |
| Day 14 | - | - |
Expand Down

0 comments on commit f30c5ad

Please sign in to comment.