Skip to content

Commit

Permalink
feat(2023): day 11 - Cosmic Expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Dec 11, 2023
1 parent bb0a974 commit a5b10c8
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 0 deletions.
120 changes: 120 additions & 0 deletions 2023/Day11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# [--- Day 11: Cosmic Expansion ---](https://adventofcode.com/2023/day/11)

You continue following signs for "Hot Springs" and eventually come across an observatory. The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here.

He doesn't know anything about the missing machine parts; he's only visiting for this research project. However, he confirms that the hot springs are the next-closest area likely to have people; he'll even take you straight there once he's done with today's observation analysis.

Maybe you can help him with the analysis to speed things up?

The researcher has collected a bunch of data and compiled the data into a single giant image (your puzzle input). The image includes empty space (.) and galaxies (#). For example:

```txt
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
```

The researcher is trying to figure out the sum of the lengths of the shortest path between every pair of galaxies. However, there's a catch: the universe expanded in the time it took the light from those galaxies to reach the observatory.

Due to something involving gravitational effects, only some space expands. In fact, the result is that any rows or columns that contain no galaxies should all actually be twice as big.

In the above example, three columns and two rows contain no galaxies:

```txt
v v v
...#......
.......#..
#.........
>..........<
......#...
.#........
.........#
>..........<
.......#..
#...#.....
^ ^ ^
```

These rows and columns need to be twice as big; the result of cosmic expansion therefore looks like this:

```txt
....#........
.........#...
#............
.............
.............
........#....
.#...........
............#
.............
.............
.........#...
#....#.......
```

Equipped with this expanded universe, the shortest path between every pair of galaxies can be found. It can help to assign every galaxy a unique number:

```txt
....1........
.........2...
3............
.............
.............
........4....
.5...........
............6
.............
.............
.........7...
8....9.......
```

In these 9 galaxies, there are 36 pairs. Only count each pair once; order within the pair doesn't matter. For each pair, find any shortest path between the two galaxies using only steps that move up, down, left, or right exactly one . or # at a time. (The shortest path between two galaxies is allowed to pass through another galaxy.)

For example, here is one of the shortest paths between galaxies 5 and 9:

```txt
....1........
.........2...
3............
.............
.............
........4....
.5...........
.##.........6
..##.........
...##........
....##...7...
8....9.......
```

This path has length 9 because it takes a minimum of nine steps to get from galaxy 5 to galaxy 9 (the eight locations marked # plus the step onto galaxy 9 itself). Here are some other example shortest path lengths:

- Between galaxy `1` and galaxy `7`: `15`
- Between galaxy `3` and galaxy `6`: `17`
- Between galaxy `8` and galaxy `9`: `5`

In this example, after expanding the universe, the sum of the shortest path between all `36` pairs of galaxies is `374`.

Expand the universe, then find the length of the shortest path between every pair of galaxies. **What is the sum of these lengths?**

> Your puzzle answer was `9799681`.
## --- Part Two ---

The galaxies are much older (and thus much farther apart) than the researcher initially estimated.

Now, instead of the expansion you did before, make each empty row or column one million times larger. That is, each empty row should be replaced with `1000000` empty rows, and each empty column should be replaced with `1000000` empty columns.

(In the example above, if each empty row or column were merely `10` times larger, the sum of the shortest paths between every pair of galaxies would be `1030`. If each empty row or column were merely `100` times larger, the sum of the shortest paths between every pair of galaxies would be `8410`. However, your universe will need to expand far beyond these values.)

Starting with the same initial image, expand the universe according to these new rules, then find the length of the shortest path between every pair of galaxies. **What is the sum of these lengths?**

> Your puzzle answer was `513171773355`.
81 changes: 81 additions & 0 deletions 2023/Day11/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
interface IEmptySpaces {
xIndices: number[];
yIndices: number[];
}

export function findEmptySpaces(inputs: string[]): IEmptySpaces {
const xIndices: number[] = [];
const yIndices: number[] = [];

// Horizontal scan
for (let y = 0; y < inputs.length; y++) {
if (!inputs[y].includes("#")) {
yIndices.push(y);
}
}

// Vertical scan
for (let x = 0; x < inputs[0].length; x++) {
if (!inputs.some((row) => row[x] === "#")) {
xIndices.push(x);
}
}

return { xIndices, yIndices };
}

export function findGalaxies(inputs: string[]): [number, number][] {
const stars: [number, number][] = [];

for (let y = 0; y < inputs.length; y++) {
for (let x = 0; x < inputs[y].length; x++) {
const cell = inputs[y][x];
if (cell === "#") {
stars.push([x, y]);
}
}
}

return stars;
}

export function getDistanceBetweenStars(
[x1, y1]: [number, number],
[x2, y2]: [number, number],
{ xIndices, yIndices }: IEmptySpaces,
expansion = 1_000_000,
): number {
const [xMin, xMax] = [x1, x2].sort((a, b) => a - b);
const [yMin, yMax] = [y1, y2].sort((a, b) => a - b);

// if there exists empty space between the two stars, add expansion to distance
const xIntersection = xIndices.filter((x) => xMin < x && x < xMax).length;
const yIntersection = yIndices.filter((y) => yMin < y && y < yMax).length;

// need to subtract 1 from the intersection count because we are replacing the row/col with n rows/cols
const xOffset = xIntersection * expansion - xIntersection;
const yOffset = yIntersection * expansion - yIntersection;

return Math.abs(x2 - x1) + xOffset + Math.abs(y2 - y1) + yOffset;
}

export function sumOfDistancesBetweenGalaxies(
galaxies: [number, number][],
emptySpaces: IEmptySpaces,
expansion = 2,
): number {
let sum = 0;

for (let i = 0; i < galaxies.length - 1; i++) {
for (let j = i + 1; j < galaxies.length; j++) {
sum += getDistanceBetweenStars(
galaxies[i],
galaxies[j],
emptySpaces,
expansion,
);
}
}

return sum;
}
12 changes: 12 additions & 0 deletions 2023/Day11/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from "path";

import { getInputLines } from "../utils/input";
import { profileRun } from "../utils/timings";
import { part1, part2 } from "./solutions";

const rawInputs = getInputLines(path.join(__dirname, `./input.txt`));

(async () => {
await profileRun("Part 1", () => part1(rawInputs));
await profileRun("Part 2", () => part2(rawInputs));
})();
Loading

0 comments on commit a5b10c8

Please sign in to comment.