day(18): refactor a bit
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
06243918fc
commit
05f9ffa320
1 changed files with 35 additions and 35 deletions
|
@ -20,26 +20,20 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_bounding_box(droplets: &[Coord]) -> (Coord, Coord) {
|
fn get_bounding_box(droplets: &[Coord]) -> (Coord, Coord) {
|
||||||
let min_coord =
|
fn find(init: i32, comparator: &dyn Fn(i32, i32) -> i32, droplets: &[Coord]) -> Coord {
|
||||||
droplets
|
droplets
|
||||||
.iter()
|
.iter()
|
||||||
.fold(Coord::new(i32::MAX, i32::MAX, i32::MAX), |acc, &droplet| {
|
.fold(Coord::new(init, init, init), |acc, &droplet| {
|
||||||
Coord::new(
|
Coord::new(
|
||||||
min(acc.x(), droplet.x()),
|
comparator(acc.x(), droplet.x()),
|
||||||
min(acc.y(), droplet.y()),
|
comparator(acc.y(), droplet.y()),
|
||||||
min(acc.z(), droplet.z()),
|
comparator(acc.z(), droplet.z()),
|
||||||
)
|
)
|
||||||
});
|
})
|
||||||
let max_coord =
|
}
|
||||||
droplets
|
|
||||||
.iter()
|
let min_coord = find(i32::MAX, &min, droplets);
|
||||||
.fold(Coord::new(i32::MIN, i32::MIN, i32::MIN), |acc, &droplet| {
|
let max_coord = find(i32::MIN, &max, droplets);
|
||||||
Coord::new(
|
|
||||||
max(acc.x(), droplet.x()),
|
|
||||||
max(acc.y(), droplet.y()),
|
|
||||||
max(acc.z(), droplet.z()),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
(min_coord, max_coord)
|
(min_coord, max_coord)
|
||||||
}
|
}
|
||||||
|
@ -63,6 +57,30 @@ enum Cube {
|
||||||
Obsidian,
|
Obsidian,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn construct_space(min_coords: Coord, max_coords: Coord) -> BTreeMap<Coord, Cube> {
|
||||||
|
let mut space = BTreeMap::<Coord, Cube>::new();
|
||||||
|
|
||||||
|
for cube in [
|
||||||
|
min_coords.x()..=max_coords.x(),
|
||||||
|
min_coords.y()..=max_coords.y(),
|
||||||
|
min_coords.z()..=max_coords.z(),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.multi_cartesian_product()
|
||||||
|
.map(|coords| {
|
||||||
|
let (x, y, z) = coords
|
||||||
|
.into_iter()
|
||||||
|
.collect_tuple::<(i32, i32, i32)>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Coord::new(x, y, z)
|
||||||
|
}) {
|
||||||
|
space.insert(cube, Cube::Lava);
|
||||||
|
}
|
||||||
|
|
||||||
|
space
|
||||||
|
}
|
||||||
|
|
||||||
fn aerate(space: &mut BTreeMap<Coord, Cube>, start: Coord) {
|
fn aerate(space: &mut BTreeMap<Coord, Cube>, start: Coord) {
|
||||||
let mut queue = VecDeque::<Coord>::new();
|
let mut queue = VecDeque::<Coord>::new();
|
||||||
queue.push_back(start);
|
queue.push_back(start);
|
||||||
|
@ -116,29 +134,11 @@ impl Solution<Input, Output> for Day18 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(input: &Input) -> Output {
|
fn part_2(input: &Input) -> Output {
|
||||||
let mut space = BTreeMap::<Coord, Cube>::new();
|
|
||||||
|
|
||||||
_check_bounding_box(input); // for debugging purposes
|
_check_bounding_box(input); // for debugging purposes
|
||||||
let (min_coords, max_coords) = get_bounding_box(input);
|
let (min_coords, max_coords) = get_bounding_box(input);
|
||||||
|
|
||||||
// lava everywhere
|
// lava everywhere
|
||||||
for cube in [
|
let mut space = construct_space(min_coords, max_coords);
|
||||||
min_coords.x()..=max_coords.x(),
|
|
||||||
min_coords.y()..=max_coords.y(),
|
|
||||||
min_coords.z()..=max_coords.z(),
|
|
||||||
]
|
|
||||||
.into_iter()
|
|
||||||
.multi_cartesian_product()
|
|
||||||
.map(|coords| {
|
|
||||||
let (x, y, z) = coords
|
|
||||||
.into_iter()
|
|
||||||
.collect_tuple::<(i32, i32, i32)>()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Coord::new(x, y, z)
|
|
||||||
}) {
|
|
||||||
space.insert(cube, Cube::Lava);
|
|
||||||
}
|
|
||||||
|
|
||||||
// override with obsidian
|
// override with obsidian
|
||||||
for &droplet in input {
|
for &droplet in input {
|
||||||
|
|
Loading…
Reference in a new issue