Closed simonw closed 1 year ago
First change this:
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
Into a diagram like this:
4 5 5
9 0 0
4 0 3
0 ......+...
1 ..........
2 ..........
3 ..........
4 ....#...##
5 ....#...#.
6 ..###...#.
7 ........#.
8 ........#.
9 #########.
Those lines of input turn into rock formations drawn with #
.
The next step is to simulate falling sand starting at point + (500, 0)
It would be fun to animate this one on the terminal.
Found this via https://blessed.rs - looks good! https://lib.rs/crates/crossterm
Run cargo test but show any printed lines
ChatGPT said:
To run
cargo test
and show any printed lines, you can use the--nocapture
flag when running thetest
command, like this:cargo test -- --nocapture
This tells
cargo
to run the tests in the current project, and to not capture the output of the tests. This means that anyprintln!
orprint!
statements in the test code will be printed to the terminal as the tests are running, allowing you to see the output of the tests as they are executed.Note that the
--nocapture
flag must be specified after the--
separator, which is used to indicate that the following arguments should be passed directly to thetest
command, rather than tocargo
.If you only want to show the output of a specific test, you can use the
--nocapture
flag with the--test
option, followed by the name of the test you want to run, like this:cargo test --test my_test -- --nocapture
I like how it also taught me how to use --test x
- I hadn't tried that before.
Got to this point:
day_14 % cargo test -- --nocapture
Finished test [unoptimized + debuginfo] target(s) in 0.00s
Running unittests src/main.rs (target/debug/deps/day_14-bfff42a1bd776381)
running 1 test
Drawing line from (498, 4) to (498, 6)
Setting grid[4][0] to #
Setting grid[4][1] to #
Setting grid[4][2] to #
Drawing line from (498, 4) to (496, 6)
Drawing line from (503, 4) to (502, 4)
Drawing line from (503, 4) to (502, 9)
Drawing line from (503, 4) to (494, 9)
....#.....
....#.....
....#.....
..........
..........
..........
test test_parse_grid ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
That's not quite right yet - it's not handling lines that are drawn in reverse order.
Got that working:
day_14 % cargo test -- --nocapture
Compiling day_14 v0.1.0 (/Users/simon/Dropbox/Development/advent-of-code-2022-in-rust/day_14)
Finished test [unoptimized + debuginfo] target(s) in 0.48s
Running unittests src/main.rs (target/debug/deps/day_14-bfff42a1bd776381)
running 1 test
Vertical line from 4 to 6
Horizontal line from 496 to 498
Horizontal line from 502 to 503
Vertical line from 4 to 9
Horizontal line from 494 to 502
....#...##
....#...#.
..###...#.
........#.
........#.
#########.
test test_parse_grid ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Next bit:
The sand is pouring into the cave from point 500,0.
Sand is produced one unit at a time, and the next unit of sand is not produced until the previous unit of sand comes to rest
A unit of sand always falls down one step if possible. If the tile immediately below is blocked (by rock or sand), the unit of sand attempts to instead move diagonally one step down and to the left. If that tile is blocked, the unit of sand attempts to instead move diagonally one step down and to the right
[..] If all three possible destinations are blocked, the unit of sand comes to rest and no longer moves, at which point the next unit of sand is created back at the source.
How many units of sand come to rest before sand starts flowing into the abyss below?
Answer is 24 for the example.
So algorithm inside the loop is:
Getting this error:
---- test_parse_grid stdout ----
....#...##
....#...#.
..###...#.
........#.
........#.
#########.
char_at: 500 1
translated to: 6 -3
thread 'test_parse_grid' panicked at 'index out of bounds: the len is 6 but the index is 18446744073709551613', src/main.rs:36:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
That's because the grid needs to always have 0 and up on that axis.
After first loop I get:
..........
..........
..........
..........
....#...##
....#...#.
..###...#.
........#.
......o.#.
#########.
``
Works against example:
After 22 iterations:
..........
..........
......o...
.....ooo..
....#ooo##
....#ooo#.
..###ooo#.
....oooo#.
...ooooo#.
#########.
After 23 iterations:
..........
..........
......o...
.....ooo..
....#ooo##
...o#ooo#.
..###ooo#.
....oooo#.
...ooooo#.
#########.
After 24 iterations:
..........
..........
......o...
.....ooo..
....#ooo##
...o#ooo#.
..###ooo#.
....oooo#.
.o.ooooo#.
#########.
Final i: 24
After 30:
After 30 iterations:
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................oo..............................
.........................oooo.............................
........................oooooo............................
.......................oo#####o...........................
......................oooo...ooo..........................
.....................o#####.#####o........................
....................ooo.........ooo.......................
...................#####.#####.#####......................
..........................................................
................#####.#####.#####.#####...................
..........................................................
..........................................................
............######........................................
..........................................................
..........................................................
.........######.######....................................
..........................................................
..........................................................
......######.######.######................................
..........................................................
..........................................................
...######.######.######.######............................
..........................................................
..........................................................
######.######.######.######.######........................
..........................................................
..........................................................
.....#.#..................................................
.....#.#..................................................
.#####.########...........................................
.#............#...........................................
.#............#...........................................
.#............#...........................................
.##############...........................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
...................................#...#..................
...................................#...#..................
...................................#.#.#..................
...................................#.#.#.#................
...................................#.#.#.#................
...................................#.#.#.#................
...................................#.#.#.#................
...................................#######................
..........................................................
..........................................................
..........................................................
......................................############........
..........................................................
..........................................................
..........................................................
..........................................................
......................................................#...
......................................................#...
......................................................#...
......................................................#...
............................................#.....#.#.#...
............................................#.....#.#.#...
............................................#.#...#.#.#...
............................................#.#.#.#.#.#...
............................................###########...
..........................................................
..........................................................
........................................######............
..........................................................
..........................................................
.....................................######.######........
..........................................................
..........................................................
..................................######.######.######....
..........................................................
..........................................................
...............................######.######.######.######
..........................................................
..........................................................
..................................#.....#.................
..................................#.....#.................
..................................#.....#.................
..............................#####.....#######...........
..............................#...............#...........
..............................#...............#...........
..............................#...............#...........
..............................#...............#...........
..............................#################...........
..........................................................
..........................................................
...........................#####..........................
..........................................................
..........................................................
........................#####.#####.......................
..........................................................
..........................................................
.....................#####.#####.#####....................
..........................................................
..........................................................
..................#####.#####.#####.#####.................
..........................................................
..........................................................
..........................................................
.....................#....................................
...........#.........#....................................
...........#...#.....#....................................
...........#.#.#.....#....................................
...........#.#.#...#.#....................................
...........#.#.#...#.#....................................
...........#.#.#...#.#....................................
...........#.#.#...#.#....................................
.........#.#.#.#.#.#.#.#..................................
.........###############..................................
..........................................................
..........................................................
......................#....#..............................
......................#....#..............................
......................#....#..............................
................#######....####...........................
................#.............#...........................
................#.............#...........................
................#.............#...........................
................###############...........................
..........................................................
.......................................#..................
..............................##########..................
..........................................................
..........................................................
...........................#.#............................
...................#...#...#.#.....#......................
...................#...#...#.#.....#......................
...................#.#.#...#.#.....#......................
...................#.#.#...#.#.....#......................
...................#.#.#...#.#.....#......................
...................#.#.#.#.#.#.....#.#....................
...................#.#.#.#.#.#.....#.#....................
...................#.#.#.#.#.#...#.#.#....................
...................#.#.#.#.#.#.#.#.#.#....................
...................###################....................
..........................................................
..........................................................
...................................##############.........
..........................................................
..........................................................
................................................#...#.....
................................................#...#.....
..............................................###...######
..............................................#..........#
..............................................#..........#
..............................................############
..........................................................
..........................................................
..........................................................
...................................................#......
.....................................###############......
Got the right answer for part 1:
After 828 iterations:
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................oo..............................
.........................oooo.............................
........................oooooo............................
.......................oooooooo...........................
......................oooooooooo..........................
.....................oooo#####ooo.........................
....................oooooo...ooooo........................
...................ooo#####.#####oo.......................
..................ooooo.........oooo......................
.................oo#####.#####.#####o.....................
................oooo...............ooo....................
...............o#####.#####.#####.#####...................
..............ooo.........................................
.............ooooo........................................
............######o.......................................
.................ooo......................................
................ooooo.....................................
.........######o######....................................
..............ooo.........................................
.............ooooo........................................
......######o######.######................................
...........ooo............................................
..........ooooo...........................................
...######o######.######.######............................
........ooo...............................................
.......ooooo..............................................
######o######.######.######.######........................
.....ooo..................................................
....ooooo.................................................
...oo#o#oo................................................
..ooo#o#ooo...............................................
.#####o########...........................................
.#...ooo......#...........................................
.#..ooooo.....#...........................................
.#.ooooooo....#...........................................
.##############...........................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
...................................#..o#..................
...................................#.oo#..................
...................................#o#o#o.................
...................................#o#o#o#................
...................................#o#o#o#o...............
...................................#o#o#o#oo..............
...................................#o#o#o#ooo.............
...................................#######oooo............
.........................................oooooo...........
........................................oooooooo..........
.......................................oooooooooo.........
......................................############........
..........................................................
..........................................................
..................................................o.......
.................................................ooo......
................................................ooooo.#...
...............................................ooooooo#...
..............................................oooooooo#...
.............................................ooooooooo#...
............................................#ooooo#o#o#...
............................................#ooooo#o#o#...
............................................#o#ooo#o#o#...
............................................#o#o#o#o#o#...
...........................................o###########...
..........................................ooo.............
.........................................ooooo............
........................................######o...........
.......................................o.....ooo..........
......................................ooo...ooooo.........
.....................................######o######........
....................................o.....ooo.............
...................................ooo...ooooo............
..................................######o######.######....
.................................o.....ooo................
................................ooo...ooooo...............
...............................######o######.######.######
....................................ooo...................
...................................ooooo..................
..................................#ooooo#.................
..................................#ooooo#.................
..................................#ooooo#.................
..............................#####ooooo#######...........
..............................#...ooooooo.....#...........
..............................#..ooooooooo....#...........
..............................#.ooooooooooo...#...........
..............................#ooooooooooooo..#...........
..............................#################...........
.............................o............................
............................ooo...........................
...........................#####..........................
..........................o...............................
.........................ooo..............................
........................#####.#####.......................
.......................o..................................
......................ooo.................................
.....................#####.#####.#####....................
...................oo.....................................
..................oooo....................................
.................o#####.#####.#####.#####.................
................ooo.......................................
...............ooooo......................................
..............ooooooo.....................................
.............oooooooo#....................................
...........#ooooooooo#....................................
...........#ooo#ooooo#....................................
...........#o#o#ooooo#....................................
...........#o#o#ooo#o#....................................
...........#o#o#ooo#o#oo..................................
...........#o#o#ooo#o#ooo.................................
...........#o#o#ooo#o#oooo................................
.........#.#o#o#o#o#o#o#ooo...............................
.........###############oooo..............................
.......................oooooo.............................
......................oooooooo............................
.....................o#oooo#ooo...........................
....................oo#oooo#oooo..........................
...................ooo#oooo#ooooo.........................
................#######oooo####ooo........................
................#.....oooooo..#oooo.......................
................#....oooooooo.#ooooo......................
................#...oooooooooo#oooooo.....................
................###############ooooooo....................
..............................ooooooooo...................
.............................oooooooooo#..................
............................oo##########..................
...........................oooo...........................
..........................oooooo..........................
.........................oo#o#ooo.........................
...................#...#ooo#o#oooo.#......................
...................#...#ooo#o#ooooo#......................
...................#.#.#ooo#o#ooooo#......................
...................#.#.#ooo#o#ooooo#......................
...................#.#.#ooo#o#ooooo#...oo.................
...................#.#.#o#o#o#ooooo#.#oooo................
...................#.#.#o#o#o#ooooo#.#ooooo...............
...................#.#.#o#o#o#ooo#o#.#oooooo..............
...................#.#.#o#o#o#o#o#o#.#ooooooo.............
...................###################oooooooo............
.....................................oooooooooo...........
....................................oooooooooooo..........
...................................##############.........
.................................................o........
................................................ooo.......
...............................................o#ooo#.....
..............................................oo#ooo#.....
.............................................o###ooo######
............................................oo#.ooooo....#
...........................................ooo#ooooooo...#
..........................................oooo############
.........................................oooooo...........
........................................oooooooo..........
.......................................oooooooooo.........
......................................oooooooooooo.#......
.....................................###############......
Final i: 828
assume the floor is an infinite horizontal line with a y coordinate equal to two plus the highest y coordinate of any point in your scan.
To find somewhere safe to stand, you'll need to simulate falling sand until a unit of sand comes to rest at 500,0, blocking the source entirely and stopping the flow of sand into the cave.
Using your scan, simulate the falling sand until the source of the sand becomes blocked. How many units of sand come to rest?
Tried to get that working by drawing that floor line - didn't really work, I think I'm going to try teaching the sand that it should behave differently if it hits the virtual floor instead.
Here's my diff that didn't work:
diff --git a/day_14/src/main.rs b/day_14/src/main.rs
index 21a3ed8..d9e2f08 100644
--- a/day_14/src/main.rs
+++ b/day_14/src/main.rs
@@ -80,6 +80,10 @@ impl Grid {
} else {
// Sand lives here now
self.set_char_at(sand_pos, 'o');
+ // If this is the 500,0 source, sand is now blocked
+ if sand_pos == (500, 0) {
+ return false;
+ }
break;
}
}
@@ -90,7 +94,7 @@ impl Grid {
fn main() {
let instructions = String::from(include_str!("../input.txt"));
- let mut grid = parse_grid(instructions);
+ let mut grid = parse_grid(instructions, false);
println!("{}", grid);
// Simulate sand until it falls into the void
let mut i = 0;
@@ -100,16 +104,30 @@ fn main() {
println!("{}\n\n", grid);
}
println!("Final i: {}", i);
+
+ println!("\nPart 2\n======\n");
+
+ // Now run simulation again with a floor at two plus the highest y coordinate
+ let mut grid = parse_grid(String::from(include_str!("../input.txt")), true);
+ let mut i = 0;
+ while grid.simulate_sand() {
+ i += 1;
+ }
+ println!("{}\n\n", grid);
+ println!("Final i: {}", i);
}
-fn parse_grid(input: String) -> Grid {
+fn parse_grid(input: String, has_floor: bool) -> Grid {
// Each line describes a shape of rock drawn on the grid
let mut min_x = 100000;
let mut max_x = -100000;
let mut min_y = 0;
let mut max_y = -100000;
let mut grid_lines = Vec::new();
+ if has_floor {
+ max_x = 100000;
+ }
// First loop to find min/max of everything:
for line in input.lines() {
// Line looks like this:
@@ -144,8 +162,16 @@ fn parse_grid(input: String) -> Grid {
}
grid_lines.push(line);
}
+ // If grid has floor, add an instruction drawing it
+ let mut to_process = "".to_string();
+ to_process.push_str(&input);
+ if has_floor {
+ let extra = format!("\n{},{} -> {},{}", min_x, max_y + 2, max_x, max_y + 2);
+ println!("Adding extra line: {}", extra);
+ to_process.push_str(&extra);
+ }
// Second loop to fill in the grid based on instructions
- for line in input.lines() {
+ for line in to_process.lines() {
// 498,4 -> 498,6 -> 496,6
let mut coords = line.split(" -> ");
let mut done_first = false;
@@ -217,7 +243,7 @@ fn parse_grid(input: String) -> Grid {
#[test]
fn test_parse_grid() {
let instructions = String::from("498,4 -> 498,6 -> 496,6\n503,4 -> 502,4 -> 502,9 -> 494,9");
- let mut grid = parse_grid(instructions);
+ let mut grid = parse_grid(instructions, false);
assert_eq!(grid.width, 10);
assert_eq!(grid.height, 10);
assert_eq!(grid.min_x, 494);
I didn't get this working. I'm giving up on part 2 - might revisit it some time in the future.
https://adventofcode.com/2022/day/14