part 2 solution
parent
dfcdf0f83d
commit
960a2b1e28
|
@ -166,6 +166,48 @@ fn pour_over(m: &mut Map, start_x: usize, start_y: usize) {
|
|||
}
|
||||
}
|
||||
|
||||
fn lock_water(m: &mut Map, bounds: &MapBounds) {
|
||||
for y in bounds.min_y..=bounds.max_y {
|
||||
for x in 0..MAP_SIZE {
|
||||
if m[x][y] == FALLING_WATER {
|
||||
let mut locked = true;
|
||||
for k in (0..x).rev() {
|
||||
if m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
} else if m[k][y] == EMPTY_SPACE {
|
||||
locked = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for k in (x+1)..MAP_SIZE {
|
||||
if m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
} else if m[k][y] == EMPTY_SPACE {
|
||||
locked = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if locked {
|
||||
m[x][y] = WATER;
|
||||
} else {
|
||||
for k in (0..x).rev() {
|
||||
if m[k][y] == EMPTY_SPACE || m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
}
|
||||
m[k][y] = FALLING_WATER;
|
||||
}
|
||||
for k in (x+1)..MAP_SIZE {
|
||||
if m[k][y] == EMPTY_SPACE || m[k][y] == WALL_SPACE {
|
||||
break;
|
||||
}
|
||||
m[k][y] = FALLING_WATER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn count_water(m: &Map, bounds: &MapBounds) -> usize {
|
||||
let mut squares: usize = 0;
|
||||
for y in bounds.min_y..=bounds.max_y {
|
||||
|
@ -198,9 +240,17 @@ fn count_resting_water(m: &Map, bounds: &MapBounds) -> usize {
|
|||
fn main() {
|
||||
let (mut m, bounds) = build_map_from_file("input");
|
||||
pour_over(&mut m, 500, 0);
|
||||
if cfg!(debug_assertions) {
|
||||
print_map(&m, 0, MAP_SIZE - 1, 0, MAP_SIZE - 1);
|
||||
}
|
||||
let water_squares = count_water(&m, &bounds);
|
||||
println!("found {} water squares", water_squares);
|
||||
lock_water(&mut m, &bounds);
|
||||
if cfg!(debug_assertions) {
|
||||
print_map(&m, 0, MAP_SIZE - 1, 0, MAP_SIZE - 1);
|
||||
}
|
||||
let water_squares = count_water(&m, &bounds);
|
||||
println!("found {} locked water squares", water_squares);
|
||||
let resting_squares = count_resting_water(&m, &bounds);
|
||||
println!("found {} resting water squares", resting_squares);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue