diff --git a/2022/src/days/day14.rs b/2022/src/days/day14.rs index cc5036d..ab06598 100644 --- a/2022/src/days/day14.rs +++ b/2022/src/days/day14.rs @@ -9,7 +9,7 @@ enum Tile { Sand, } -type Cave = [[Tile; 540]; 200]; +type Cave = [[Tile; 800]; 200]; pub fn run() -> Result<()> { #[cfg(not(feature = "test_input"))] @@ -18,7 +18,7 @@ pub fn run() -> Result<()> { #[cfg(feature = "test_input")] 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; let mut minx = 520; 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); + part2_sand_units(&cave, maxy); Ok(()) } @@ -134,23 +134,31 @@ fn part1_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) { 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 units = 0; + let mut minx = 800; + let mut maxx = 0; let floor = maxy + 2; - for i in 0..540 { + for i in 0..800 { cave[floor][i] = Tile::Rock; } loop { - if let Tile::Sand = cave[500][0] { + if let Tile::Sand = cave[0][500] { break; } let mut x = 500; let mut placed = false; for i in 0..floor { if placed { + if x < minx { + minx = x; + } + if x > maxx { + maxx = x; + } break; } match cave[i + 1][x] { @@ -164,7 +172,6 @@ fn part2_sand_units(c: &Cave, minx: usize, maxx: usize, maxy: usize) { } else { placed = true; 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); - print_cave(&cave, minx, 0, maxx, maxy); + print_cave(&cave, minx, 0, maxx, floor); }