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) {
|
||||
let min_coord =
|
||||
fn find(init: i32, comparator: &dyn Fn(i32, i32) -> i32, droplets: &[Coord]) -> Coord {
|
||||
droplets
|
||||
.iter()
|
||||
.fold(Coord::new(i32::MAX, i32::MAX, i32::MAX), |acc, &droplet| {
|
||||
.fold(Coord::new(init, init, init), |acc, &droplet| {
|
||||
Coord::new(
|
||||
min(acc.x(), droplet.x()),
|
||||
min(acc.y(), droplet.y()),
|
||||
min(acc.z(), droplet.z()),
|
||||
comparator(acc.x(), droplet.x()),
|
||||
comparator(acc.y(), droplet.y()),
|
||||
comparator(acc.z(), droplet.z()),
|
||||
)
|
||||
});
|
||||
let max_coord =
|
||||
droplets
|
||||
.iter()
|
||||
.fold(Coord::new(i32::MIN, i32::MIN, i32::MIN), |acc, &droplet| {
|
||||
Coord::new(
|
||||
max(acc.x(), droplet.x()),
|
||||
max(acc.y(), droplet.y()),
|
||||
max(acc.z(), droplet.z()),
|
||||
)
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
let min_coord = find(i32::MAX, &min, droplets);
|
||||
let max_coord = find(i32::MIN, &max, droplets);
|
||||
|
||||
(min_coord, max_coord)
|
||||
}
|
||||
|
@ -63,6 +57,30 @@ enum Cube {
|
|||
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) {
|
||||
let mut queue = VecDeque::<Coord>::new();
|
||||
queue.push_back(start);
|
||||
|
@ -116,29 +134,11 @@ impl Solution<Input, Output> for Day18 {
|
|||
}
|
||||
|
||||
fn part_2(input: &Input) -> Output {
|
||||
let mut space = BTreeMap::<Coord, Cube>::new();
|
||||
|
||||
_check_bounding_box(input); // for debugging purposes
|
||||
let (min_coords, max_coords) = get_bounding_box(input);
|
||||
|
||||
// lava everywhere
|
||||
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);
|
||||
}
|
||||
let mut space = construct_space(min_coords, max_coords);
|
||||
|
||||
// override with obsidian
|
||||
for &droplet in input {
|
||||
|
|
Loading…
Reference in a new issue