day14 part 2 solution
parent
bbe7c7332f
commit
5113eba2a9
|
@ -9,7 +9,7 @@ enum Tile {
|
||||||
Sand,
|
Sand,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cave = [[Tile; 540]; 200];
|
type Cave = [[Tile; 800]; 200];
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
#[cfg(not(feature = "test_input"))]
|
#[cfg(not(feature = "test_input"))]
|
||||||
|
@ -18,7 +18,7 @@ pub fn run() -> Result<()> {
|
||||||
#[cfg(feature = "test_input")]
|
#[cfg(feature = "test_input")]
|
||||||
let file_contents = fs::read_to_string("tests/day14.txt")?;
|
let file_contents = fs::read_to_string("tests/day14.txt")?;
|
||||||
|
|
||||||
let mut cave: Cave = [[Tile::Air; 540]; 200];
|
let mut cave: Cave = [[Tile::Air; 800]; 200];
|
||||||
cave[0][500] = Tile::Source;
|
cave[0][500] = Tile::Source;
|
||||||
let mut minx = 520;
|
let mut minx = 520;
|
||||||
let mut maxx = 500;
|
let mut maxx = 500;
|
||||||
|
@ -76,8 +76,8 @@ pub fn run() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//print_cave(&cave, minx, 0, maxx, maxy);
|
|
||||||
part1_sand_units(&cave, minx, maxx, maxy);
|
part1_sand_units(&cave, minx, maxx, maxy);
|
||||||
|
part2_sand_units(&cave, maxy);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -134,23 +134,31 @@ fn part1_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
||||||
print_cave(&cave, minx, 0, maxx, maxy);
|
print_cave(&cave, minx, 0, maxx, maxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
fn part2_sand_units(c: &Cave, maxy: usize) {
|
||||||
let mut cave = c.clone();
|
let mut cave = c.clone();
|
||||||
let mut units = 0;
|
let mut units = 0;
|
||||||
|
let mut minx = 800;
|
||||||
|
let mut maxx = 0;
|
||||||
|
|
||||||
let floor = maxy + 2;
|
let floor = maxy + 2;
|
||||||
for i in 0..540 {
|
for i in 0..800 {
|
||||||
cave[floor][i] = Tile::Rock;
|
cave[floor][i] = Tile::Rock;
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Tile::Sand = cave[500][0] {
|
if let Tile::Sand = cave[0][500] {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let mut x = 500;
|
let mut x = 500;
|
||||||
let mut placed = false;
|
let mut placed = false;
|
||||||
for i in 0..floor {
|
for i in 0..floor {
|
||||||
if placed {
|
if placed {
|
||||||
|
if x < minx {
|
||||||
|
minx = x;
|
||||||
|
}
|
||||||
|
if x > maxx {
|
||||||
|
maxx = x;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
match cave[i + 1][x] {
|
match cave[i + 1][x] {
|
||||||
|
@ -164,7 +172,6 @@ fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
||||||
} else {
|
} else {
|
||||||
placed = true;
|
placed = true;
|
||||||
cave[i][x] = Tile::Sand;
|
cave[i][x] = Tile::Sand;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,5 +183,5 @@ fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("part two units {}", units);
|
println!("part two units {}", units);
|
||||||
print_cave(&cave, minx, 0, maxx, maxy);
|
print_cave(&cave, minx, 0, maxx, floor);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue