-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
253 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
...#...... | ||
.......#.. | ||
#......... | ||
.......... | ||
......#... | ||
.#........ | ||
.........# | ||
.......... | ||
.......#.. | ||
#...#..... |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
solutions/typescript/libs/lib/src/array/filter-map.function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters