Skip to content

Commit

Permalink
Solution y2023ex14
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Dec 14, 2023
1 parent 3c001fa commit 4004394
Show file tree
Hide file tree
Showing 7 changed files with 539 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ members = [
"y2023/ex11",
"y2023/ex12",
"y2023/ex13",
"y2023/ex14",
]

[profile.bench]
Expand Down
15 changes: 15 additions & 0 deletions y2023/ex14/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "y2023ex14"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bench]]
name = "bench_y2023ex14"
harness = false

[dev-dependencies]
criterion = "0.5.1"
117 changes: 117 additions & 0 deletions y2023/ex14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Day 14: Parabolic Reflector Dish

[Check it out on adventofcode.com](https://adventofcode.com/2023/day/14)

## Part One

You reach the place where all of the mirrors were pointing: a massive [parabolic reflector dish](https://en.wikipedia.org/wiki/Parabolic_reflector) 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:

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:

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:

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 `109661`


## 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:
.....#....
....#...O#
...OO##...
.OO#......
.....OOO#.
.O#...O#.#
....O#....
......OOOO
#...O###..
#..OO#....

After 2 cycles:
.....#....
....#...O#
.....##...
..O#......
.....OOO#.
.O#...O#.#
....O#...O
.......OOO
#..OO###..
#.OOO#...O

After 3 cycles:
.....#....
....#...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 `90176`
11 changes: 11 additions & 0 deletions y2023/ex14/benches/bench_y2023ex14.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use y2023ex14::{part1, part2};

fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../input.txt");
c.bench_function("y2023ex14::part1", |b| b.iter(|| part1(black_box(input))));
c.bench_function("y2023ex14::part2", |b| b.iter(|| part2(black_box(input))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
100 changes: 100 additions & 0 deletions y2023/ex14/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
..O.O....O...O...O...O.#O#.#.#O#O..#.#O....O...#..#O#...............#O..#.#..O..O.##.#O#..O...#.....
.#O.#.#O.##..O..#.......###...#.O#.##....#O#O.OO..##...#..O.O...#O.......O.O.OO##...#.##OOO.O.#..#.O
#.##..O.O.....#.OO....O#..OOO..OO..O....#..#...O..#..O..O.O#O.....O...O.#.O.O......O..OO..O...O...OO
O.....#.O...#....#.#O.#.O.........O...O..#.O.O...O#..O.OO......O.#O......O#.OO.#.##..OO.O#O.O.#.....
#O...#..........#..#......O..O..#......#O..OO...#O.O.............O..O.......#....O.OO#..OO#....O#.O.
##O.O.....OOO..OO..O...O.OO...#.O.O...#..O...OO...O..O.#OO.#........O........O.O....O...O...........
O..O...#.O.##.....OO.O#.......OO...OOO.#..#OO....#..O#...O.....O..O.#####..........O#..........O#...
OO......OO.O...#.#......#.#....OO###...O####OO..#...........##..O#O...#.........#..#....#OO.O.#....O
.##..O.#...O.#.O#..#.O.#...OO##O.....O.O.......O...........##...O....#OOO.....#...O.....O.O##.O###OO
O..O...O.O...O#O.OOOO.O.O......O#...O...#.#.......O#.O...#..#O.....#........O#...O..O..O.#.#.#O#OOO.
O#......#.#.O...OO#..#.#.#.O#.O#.......O#...O.O..O..........O........#.#O....#..OO..#..O..#.O..##...
OOO.#.OO.O..OO.OO..O....O...OO..#..#OO.O...#....#.OO.#..O.O#.O.#OO.O.....O......#.O..O#O...OO##.#...
.O#O..#OOOO#.O##.........OO#O#.#.O#..#..OO.....O.##.#..O.O....#...#..O....O.#.O#..O....O.O.....O.O..
..O#.OO..........#OO...O..O...#.####.#...#.......#####O.#.##..#O...O...O.....O......#.O.....O.......
.O........#.O.###OO..............#.......O...O.....OO#...#.O...#.O.O.O.O.......#.O..#O....##..#..O.#
.#.O#..#....O...#..O.#.O....O#......#...#...OO.O....#.O.O..#.......##OO..O.....OO...OO#...OO...###..
.#O.#.#...O.#..#.......#....#O.O.#....OO.O##.O.#..OO.O......##.....OO...O#..O.O.OO.#.O.O....#.......
O..O.OO.O.....O..O..#....O.......O.##O#...O.....O#OO#.....O..O.....O..#..O...............##.....#O..
....O..#OO#..#.#.O.#.#.OOO.OO#....OOO#....#.........O..O.#....#....OO.O.##...##O.O..#.O..#....O..O#.
#O.O.O.O.#O##...O..O##.O#....#......O...#.O..O.O....O.##O....O..#...O....##.#O.O#O...#.O#.#...O..O..
#..O.O..O##.O..O....O..OO#.OO........O#....O....O............O#.#.OO..#.#O.#....##.....OO..O...O...O
.O..OO.OOOO.#.#.O.#.O..O##....##O..##..O.#O.O#.#...#....#.....#OO..O#O......O........O...O........#.
...OO.O###.O.###.......#O#..O..#.#O...O#...O...O#..##..OO.O...O......O....#.#....OO.O.O#...OO...##O#
#..OO.....#.O.OOO.#...O.......O......#O...##...#O##..OO.#.O.........O.O...O...O..##...O..#..O#.....O
...O.#.#.O....##O...#O#...O....O..OO..#O.O....#O..#...O.OO.....OO.OOO.O.O.#.....OO#O.##..#..#.O#...O
..O#.O......##O#.OO..........#......#.OOOOO...#.O##......O...O.#OOO.O#O..#..O....#...O....#O..O.O...
...#...##O.O......#O...O#O...##OO.O......O..#O#..O.....#.OOO..#.O.O#..###.O....O.O.OOO....OO#O....#.
O#O..##..#O.#..........OO...O.O..###.O..O#.O.O.O.#.#.O.#.O...O.OO....#.#.....##..OOO#..OO......O#.##
O..O#O..#.#O.OO...#.OO.OO.##.O....#.....##O..O#..O.O.OO##.O#O.O#.O.#O..O.#.O....O#.#.O....#..OO....O
.O..##O#OO...O...O.....O....O.O.O........O........##O#OO.O.OO.O.##...##......O....#.O.O....O#.O...#.
.O.......#.#...O.....O...#........O#....O#.OO....#...O#.#O...#..##.#....##.OOO.#......#..O...OOO.#.O
......#O..##........##O#.......###.....O..OOO.......#.OO.#.....#...#.##.#..O......#O...O.O..O.....O.
.OOO#..#.....#.....#......O#O#....O.OOOO###.....O....OO.#O#...#.OO......O.O........#OO#.O..O........
#O#.#.OOO###...O..OO.O...#.#........#.#.#......OO#.....O...O....O.#....#..#..##.#.OOO#....#.#.O....O
#O#.#.##....#.O....##.....##.O.O.OO#.....#O...O#.....#..O.....#.#......#O..O...O.....#..O....#...#..
.O.......#.....O.O#.#.O.....##..#OO.#OO.O.O.#O.O.#O##OO.#..#O.O.....#......O#........#.#O...#.#.O...
#..O.##......O...O.O#..OO#...#..O..#OO.#O.O#....O#..#....O....O..#O..#.#......O...#..OO#....O.O....O
.#......#.#......#O............#OOO......#.OO..O#.O#.......#....#....##.O.......#...O.O#...O...#O..#
O...OO..O#......#.O........#.#....OO.#......O....O##..........O#..#....#O...O#.O..#....OOO..O....#O#
O...O...#.O.O.O.OOO..OO.##.O....O......#.#O...#...O...O#....O.#...#.......#.##....#.#.O.OOO..O...O.O
.#.O#...##O#.O..O....O.#.....#O.....#.O.....#...O#.O.O..#....O#OO..OOO..#O.OO.O...OO.OOO.#...##.O..#
........O#O#...#..OOO#...#O.O#..#.O.....O.OO.O#O..##.O.....O.#OO.........O.#OO.....OOO.....O.O.O.#..
.#....OO.#............O#..#.....OO....O.O.#..#...OO..#....#...#.#O....##..O....#O..#OO..O.....O....#
O..#.....O.O...O........O....#.#O...#......O....O......O......O..#.OO......#O...#..O.##......O#....O
...O.###..#...#.#..O.#.............#OO.#...#...##.#.O#.........O..##.##.#....O.....O.......O..O...O.
.O##....O...#....OO.##.#...O......OO..........#.O.......OO#.O.#..#.OO...#......OO#O.......#..#...#..
.#.O..#..#.OO.....O..O#.O..O....#.....O#O.OO.#.OOOO.##.O..O...O.O..#....O.....O.#...O.#...#.....O..#
.#.#O..#....#O..O.OO..O.....O....###..#...O.#....O#O...#....##...##.O.##..O...OOO.#..#O.....OOO#.O..
O..#O....#..............O.O.O..O.O.#..........O.....O#.O#.O..O.#.......O.OO...#.....##.#..O.#.#.#..#
#O..O..#.O.O..O#.OO....#O..#.....O..#..OO.#..##O.........#...O.#.....#..#.OO..O.##...#...#O.#O##..OO
#.........O...#..O.##O......#.O..##..#O..........#.#O#O........##....O....O....#..#...#O......O#.O.#
#.#..O.O.O.#.OO#.#..O....O#O.O.O.O..#....#..#.....#..O......O#...O.O.O....OOO##O.###.....O.O#.#..#.#
O...#OO..#...O..O..#..OOO.#....#..#.##...#..#....OO.O.O............#.#O#O#OO.OO...O.##O.##O#O....OOO
O..O.......O.#OO.O#........O###...O........O..#.O.OO.O...OO..OO.#O.O.#O.OO.O....OOO#.#.O.......O.O.O
#O..#O.......#..##.#....O.O#O#O.O#OO..O...O.O.O.OO.#..O#.......OO.O.##.O....O...#O......O....#O.O...
#..O........#..#..#O.OO##..OO...#..O#.O##....#.#.#.#...#....O.....O.....O...OO#O...#..O....#..##..O.
..O.....O.OOO#O....#.#.......#.OO....O.....OO#..O.O#.#.O...#...#..#.#OO..O.....#.O.....#...O...#.O#.
#.....#.....#O.#.O#O.O....#.....O#O#O#OO...O.....O#.........O..#....O.....O.......O.O#OO##....O#....
OO....#..O.#.O#....O#.O..#.#....#.#..#...#....#..OO......OO.O..###O....OOO.O..OOO#.#.O...#.OO.#..O#.
.##.#O...#......####......#...#..#.O.......O#..#.#...O#...O..O.........O##.#....O#OO..OO......O..##.
...O..#.O.O..#O..#O............#O#O..#.#...O..#......O..O...O##....#.#.............O..#...#......#..
#.#..#...##....#O....O.O.O.....#...#O..O.#..#.#O.O.#OOO.....#..#..#OO..O#........#.#.O..........O...
...#.O#......#...O...OO..#.#O.O..###O#O#...#.#..#....O#..O.#OO...#...O.##O...#.O..O#..#.O.#..#..OO..
...#....O...#.#.......OO#..OO.O..####.....##O.#...#.O....O......#......O#...#..#.O..O.OOO.O......#..
#..##..O..O....O..#O.O.O..O#.#.#..O..#.#...O.......OO#.OO...O..#.O#..O...OOO..O...O#..O....#....O...
.#.#...#.O.O..O..#..O........O....O#.#...O.O...#...O.O##O....#.#.O...#.O..#OOO..O.....#.O....##..##.
.......#OO..O..#.#....O.....O#.OO.O....O.........##.....OO......O...O#.....##..O#...#....OOO...##...
..#O...O.....O.O.##O..O.....O...O#O......#.........O..OO.....O.#.#OO..#....O.#...O..#.....#..O...O#.
.....O.O...#O.##.#O#..#.OO#.O..#O.O.#.OO.#OO...OO#O.##.O.#.O.##.#..#.#.............O.O.#.O..O..#...#
...O.#.....#.#.#..OO.O.O..#.O.....#.##......OO.#....OO.#.#O..#.O.....#.....##..O.#O#..OO.........#..
.O.#O.........#O....O.O....#.O.O..#.O#O..O.#.OO......O#....O#O.O.............O..#...O.OO#...O..O...O
.......#...#....#.....O......O.OO.O....O#.O...#....#...........#.O#..#..OOO.#O..#.O...O#..##.....O.#
........#....##...O..O.............#.O...O..#..#...O.##.#...O...#...#.O..#....###.#O.#.O.O..O....O.O
O.#....OOO..O.O.##.........O...O...OO#.....#O..OO..#.....#O..O#.O#.#O.#OO#...O#O....O...O...O......O
.O..........##.O.#..OO....#.O..........O.#....#.O...O.O.....OO.#..O...O....##...#OO..#...OO#.O#O.#..
O......O#O....#.##O.##OO.#....OO.....O.O.OO..O#.....#..OO.O..OO......#..O.OO..OO..O...#..O...#O.....
.###....O.....#O..#O....O.....#.OO..##....#.....O.#......#.....O##O...OO...O##O#....O#.........O#O#.
OO.#O##....#O.#...O........OO.....#..O...O..#.O#.#.#..OO#O.##.....#.....O.O....OO.....O.O....#...O#.
....O#OO..OO......#O.....#.O...OOO..OOO.#O....O..#.#.###..O..O..O#..........#..OOO...OO.O#O.#.#..#O.
O....#O.O.O.......#......O.......O.O...O...O....##O..O.OO#O....O.#............O..#O.##...O..........
O.......O....O##....#......OO..##.O....#O..##..O......O.O.......O#.#...###..#.....##..##.....O......
.O..O###O.........#.O.....OO..O.#.O.O...#O#.O.O..##..O..O...O..#...#O.#..#..O..#....O..O.O#.O#.O.OO.
..#.........O#..#OO......O.O#..O#.........O.#OOOO.....O..#O..#..#..OO.OO...OO...#..OO#...##..O...#..
..OO..#....O..O.O..OOO..#O#O##..#...O#O...#....O..O..#.O.OO..O.O......#O........#.....O.#...O#O...OO
..#......O##O.....OO...##......O...OO#....O..#OO#..#.O..O.#.......#..O.#.O#....#....#.OOO.#.....#...
..#.O.O.O.#....#.#.O.##.....#.O#.OO..OOO..O......O.........#....O..#..O.###.#..##O#...O#.O.O.O.#...#
#..O..O..#.#.O...#O#..O.....#O.O.....#............O..#.O#.....O....O..O.OO.O..#....OO.....###..#....
.O.O.#.....O#O##...#..#...#........#..O#.....O..#..O...O.#.....OO..O.#.##....OO#.#.....O..O....O.O..
........O..O.#..O#O...#........O.#O#...O#O##......O..#....#...O...O..O.O..##.OO....#.....#.O...#O...
.O.O..##O..O...OO...O..O.O#O...OOOO..O....O........O#...O..O#....O...O#OOO.O##....##..O.....OO.O#..#
.....#O.#..O......O...OO.O..#..#O....O....O....O#....O.O..O...#O.#....##......##..#..#O..#..OO...O.O
...#O.#.#..OO..O..........##..O#.#OO.......#...#O###.#O..O#O...O...#..O....O#O#O.#.....O.O.O#...O..#
##..#......O##..O.#...O....OOOO.....#....O.#...#.........#..O.#..O.....#...O...O.......O#......O#O..
.....O......O..##O#.....#O#...#O....O..O..O.......O#O##....OOOO..##...O.O#...OOOO...#OOOO##O..##....
#...O.O.O#..OOO...#O..O##.....#.#..#O...O###OO...O.OO.......O.O..#...###...........O..#....#.##...OO
O.OO......#....#........O#.#.#O.....#..........O#.#..##O...#..OO...#OO.#..O.#O..#........O.O..O#.O##
#.##...O.#.....OO...O..#.#.O.O.O.#.#O...#....#.#......#...#...#.O.#O#..#..O...#.OOO..#OO##..OO...#.#
O.....#.....#.O.O...O#.#...OO..OOO..O..O.O.#...O..#....O.#..OOO.O.O#O..OO#O..OO.O...O.........##.O..
..#....O........#.#O....O..#....O.###OO..#.#O.O..O.....###..OO.O..##...#.#..#O..O.....O.#.#O...O....
.O.OO..O#........O#..O..##...#O.......#..#.....#.........OO..#.O..OO#O..#....#OO......O............O
71 changes: 71 additions & 0 deletions y2023/ex14/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use models::Map;

mod models;

pub fn part1(input: &str) -> usize {
let mut map: Map = input.parse().unwrap();
map.tilt_north();
map.total_load()
}

pub fn part2(input: &str) -> usize {
let mut map: Map = input.parse().unwrap();
// from observing the data it seems that after a few cycles (depending on the input)
// the total load stabilises and keeps rotating between a set of fixed values.
//
// idea:
// - we do 100 cycles to let the system stabilise
// - we take the load value (starting load value)
// - we collect a repeating sequence of load values until we find a duplicate of the starting load value
// - we take the difference between the two indices and we have the cycle length
// - we take the remainder of (1_000_000_000 - 100) / cycle_length
// - we take the load value from the repeating sequence at that index
for _ in 0..100 {
map.cycle();
}
let starting_load = map.total_load();
let mut repeating_loads = vec![starting_load];
loop {
map.cycle();
let new_load = map.total_load();
if new_load == starting_load {
break;
} else {
repeating_loads.push(new_load);
}
}
let cycle_length = repeating_loads.len();
let remainder = (1_000_000_000 - 100) % cycle_length;
repeating_loads[remainder]
}

#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = include_str!("../input.txt");
const EXAMPLE_INPUT: &str = "O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....";

#[test]
fn test_part1() {
assert_eq!(part1(INPUT), 109661);
}

#[test]
fn test_part2_example() {
assert_eq!(part2(EXAMPLE_INPUT), 64);
}

#[test]
fn test_part2() {
assert_eq!(part2(INPUT), 90176);
}
}
Loading

0 comments on commit 4004394

Please sign in to comment.