-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): day 14 - Parabolic Reflector Dish
- Loading branch information
Showing
7 changed files
with
547 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# [--- Day 14: Parabolic Reflector Dish ---](https://adventofcode.com/2023/day/14) | ||
|
||
You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain. | ||
|
||
The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction. | ||
|
||
This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production. | ||
|
||
Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish. | ||
|
||
In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you tilt it in one of four directions! The rounded rocks (O) will roll when the platform is tilted, while the cube-shaped rocks (#) will stay in place. You note the positions of all of the empty spaces (.) and rocks (your puzzle input). For example: | ||
|
||
```txt | ||
O....#.... | ||
O.OO#....# | ||
.....##... | ||
OO.#O....O | ||
.O.....O#. | ||
O.#..O.#.# | ||
..O..#O..O | ||
.......O.. | ||
#....###.. | ||
#OO..#.... | ||
``` | ||
|
||
Start by tilting the lever so all of the rocks will slide north as far as they will go: | ||
|
||
```txt | ||
OOOO.#.O.. | ||
OO..#....# | ||
OO..O##..O | ||
O..#.OO... | ||
........#. | ||
..#....#.# | ||
..O..#.O.O | ||
..O....... | ||
#....###.. | ||
#....#.... | ||
``` | ||
|
||
You notice that the support beams along the north side of the platform are damaged; to ensure the platform doesn't collapse, you should calculate the total load on the north support beams. | ||
|
||
The amount of load caused by a single rounded rock (O) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (#) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows: | ||
|
||
```txt | ||
OOOO.#.O.. 10 | ||
OO..#....# 9 | ||
OO..O##..O 8 | ||
O..#.OO... 7 | ||
........#. 6 | ||
..#....#.# 5 | ||
..O..#.O.O 4 | ||
..O....... 3 | ||
#....###.. 2 | ||
#....#.... 1 | ||
``` | ||
|
||
The total load is the sum of the load caused by all of the rounded rocks. In this example, the total load is `136`. | ||
|
||
Tilt the platform so that the rounded rocks all roll north. Afterward, **what is the total load on the north support beams?** | ||
|
||
> Your puzzle answer was `108792`. | ||
## --- Part Two --- | ||
|
||
The parabolic reflector dish deforms, but not in a way that focuses the beam. To do that, you'll need to move the rocks to the edges of the platform. Fortunately, a button on the side of the control panel labeled "spin cycle" attempts to do just that! | ||
|
||
Each cycle tilts the platform four times so that the rounded rocks roll north, then west, then south, then east. After each tilt, the rounded rocks roll as far as they can before the platform tilts in the next direction. After one cycle, the platform will have finished rolling the rounded rocks in those four directions in that order. | ||
|
||
Here's what happens in the example above after each of the first few cycles: | ||
|
||
After 1 cycle: | ||
|
||
```txt | ||
.....#.... | ||
....#...O# | ||
...OO##... | ||
.OO#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#.... | ||
......OOOO | ||
#...O###.. | ||
#..OO#.... | ||
``` | ||
|
||
After 2 cycles: | ||
|
||
```txt | ||
.....#.... | ||
....#...O# | ||
.....##... | ||
..O#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#...O | ||
.......OOO | ||
#..OO###.. | ||
#.OOO#...O | ||
``` | ||
|
||
After 3 cycles: | ||
|
||
```txt | ||
.....#.... | ||
....#...O# | ||
.....##... | ||
..O#...... | ||
.....OOO#. | ||
.O#...O#.# | ||
....O#...O | ||
.......OOO | ||
#...O###.O | ||
#.OOO#...O | ||
``` | ||
|
||
This process should work if you leave it running long enough, but you're still worried about the north support beams. To make sure they'll survive for a while, you need to calculate the total load on the north support beams after `1000000000` cycles. | ||
|
||
In the above example, after `1000000000` cycles, the total load on the north support beams is `64`. | ||
|
||
Run the spin cycle for `1000000000` cycles. **Afterward, what is the total load on the north support beams?** | ||
|
||
> Your puzzle answer was `99118`. |
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,220 @@ | ||
export function parseInput(inputs: string[]): string[][] { | ||
return inputs.map((input) => input.split("")); | ||
} | ||
|
||
export enum Direction { | ||
North = "N", | ||
East = "E", | ||
South = "S", | ||
West = "W", | ||
} | ||
|
||
export class Platform { | ||
constructor(private grid: string[][]) {} | ||
|
||
public get width(): number { | ||
return this.grid[0].length; | ||
} | ||
|
||
public get height(): number { | ||
return this.grid.length; | ||
} | ||
|
||
public get(x: number, y: number): string { | ||
return this.grid[y][x]; | ||
} | ||
|
||
public set(x: number, y: number, value: string): void { | ||
this.grid[y][x] = value; | ||
} | ||
|
||
public toString(): string { | ||
return this.grid.map((row) => row.join("")).join("\n"); | ||
} | ||
|
||
public print(): void { | ||
console.log(this.toString()); | ||
} | ||
|
||
public tilt(direction: Direction): string[][] { | ||
switch (direction) { | ||
case Direction.North: | ||
this.tiltNorth(); | ||
break; | ||
case Direction.East: | ||
this.tiltEast(); | ||
break; | ||
case Direction.South: | ||
this.tiltSouth(); | ||
break; | ||
case Direction.West: | ||
this.tiltWest(); | ||
break; | ||
} | ||
return this.grid; | ||
} | ||
|
||
public getTotalLoad(direction: Direction): number { | ||
switch (direction) { | ||
case Direction.North: | ||
return this.getTotalLoadNorth(); | ||
case Direction.East: | ||
return this.getTotalLoadEast(); | ||
case Direction.South: | ||
return this.getTotalLoadSouth(); | ||
case Direction.West: | ||
return this.getTotalLoadWest(); | ||
} | ||
} | ||
|
||
public spinCycle(count: number): void { | ||
const map = new Map<string, number>(); | ||
let lengthOfCycle = 0; | ||
|
||
for (let i = 0; i < count; i++) { | ||
this.spin(); | ||
|
||
const key = this.toString(); | ||
if (!map.has(key)) { | ||
map.set(key, i); | ||
continue; | ||
} | ||
|
||
if (!lengthOfCycle) { | ||
lengthOfCycle = map.get(key) - map.size; | ||
} | ||
|
||
if (count % lengthOfCycle === (i + 1) % lengthOfCycle) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private spin(): void { | ||
this.tiltNorth(); | ||
this.tiltWest(); | ||
this.tiltSouth(); | ||
this.tiltEast(); | ||
} | ||
|
||
private tiltNorth(): void { | ||
for (let x = 0; x < this.width; x++) { | ||
let y = this.height - 1; | ||
while (y >= 0) { | ||
let storeCount = 0; | ||
// count the number of rounded rock until we hit a cubed rock | ||
while (y >= 0 && this.get(x, y) !== "#") { | ||
if (this.get(x, y) === "O") { | ||
storeCount++; | ||
this.set(x, y, "."); | ||
} | ||
y--; | ||
} | ||
|
||
// move the rounded rocks towards the bottom | ||
for (let i = 0; i < storeCount; i++) { | ||
this.set(x, y + 1 + i, "O"); | ||
} | ||
y--; | ||
} | ||
} | ||
} | ||
|
||
private tiltSouth(): void { | ||
for (let x = 0; x < this.width; x++) { | ||
let y = 0; | ||
while (y < this.height) { | ||
let storeCount = 0; | ||
// count the number of rounded rock until we hit a cubed rock | ||
while (y < this.height && this.get(x, y) !== "#") { | ||
if (this.get(x, y) === "O") { | ||
storeCount++; | ||
this.set(x, y, "."); | ||
} | ||
y++; | ||
} | ||
|
||
// move the rounded rocks towards the bottom | ||
for (let i = 0; i < storeCount; i++) { | ||
this.set(x, y - 1 - i, "O"); | ||
} | ||
y++; | ||
} | ||
} | ||
} | ||
|
||
private tiltWest(): void { | ||
for (let y = 0; y < this.height; y++) { | ||
let x = this.width - 1; | ||
while (x >= 0) { | ||
let storeCount = 0; | ||
// count the number of rounded rock until we hit a cubed rock | ||
while (x >= 0 && this.get(x, y) !== "#") { | ||
if (this.get(x, y) === "O") { | ||
storeCount++; | ||
this.set(x, y, "."); | ||
} | ||
x--; | ||
} | ||
|
||
// move the rounded rocks towards the bottom | ||
for (let i = 0; i < storeCount; i++) { | ||
this.set(x + 1 + i, y, "O"); | ||
} | ||
x--; | ||
} | ||
} | ||
} | ||
|
||
private tiltEast(): void { | ||
for (let y = 0; y < this.height; y++) { | ||
let x = 0; | ||
while (x < this.width) { | ||
let storeCount = 0; | ||
// count the number of rounded rock until we hit a cubed rock | ||
while (x < this.width && this.get(x, y) !== "#") { | ||
if (this.get(x, y) === "O") { | ||
storeCount++; | ||
this.set(x, y, "."); | ||
} | ||
x++; | ||
} | ||
|
||
// move the rounded rocks towards the bottom | ||
for (let i = 0; i < storeCount; i++) { | ||
this.set(x - 1 - i, y, "O"); | ||
} | ||
x++; | ||
} | ||
} | ||
} | ||
|
||
private getTotalLoadNorth(): number { | ||
let totalLoad = 0; | ||
|
||
for (let y = 0; y < this.height; y++) { | ||
for (let x = 0; x < this.width; x++) { | ||
if (this.get(x, y) === "O") { | ||
totalLoad += this.height - y; | ||
} | ||
} | ||
} | ||
|
||
return totalLoad; | ||
} | ||
|
||
// Not needed for this puzzle | ||
private getTotalLoadSouth(): number { | ||
return 0; | ||
} | ||
|
||
// Not needed for this puzzle | ||
private getTotalLoadWest(): number { | ||
return 0; | ||
} | ||
|
||
// Not needed for this puzzle | ||
private getTotalLoadEast(): number { | ||
return 0; | ||
} | ||
} |
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,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)); | ||
})(); |
Oops, something went wrong.