diff options
author | Irene Knapp <ireneista@gmail.com> | 2021-12-15 22:33:31 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2021-12-15 22:33:31 -0800 |
commit | 268e1a82fa61531edaaec472dbd5cd7df044176d (patch) | |
tree | 096c552ca2b5973350c12783ec8fba1f6e54f444 /15 | |
parent | ea68d068cfa85d3e5ca5efe44bf470554a2c4006 (diff) |
16, yay
Diffstat (limited to '15')
-rw-r--r-- | 15/src/main.rs.bak | 181 | ||||
-rw-r--r-- | 15/tests/main.rs | 2 |
2 files changed, 1 insertions, 182 deletions
diff --git a/15/src/main.rs.bak b/15/src/main.rs.bak deleted file mode 100644 index 33f76e3..0000000 --- a/15/src/main.rs.bak +++ /dev/null @@ -1,181 +0,0 @@ -use advent_lib::prelude::*; - - -fn main() -> Result<()> { - let mut args = std::env::args(); - if args.len() != 2 { - eprintln!("Usage: advent input"); - } - let _ = args.next(); - let filename = args.next().unwrap(); - - let input = advent_lib::read_lines_file(&filename)?; - - let mut value_grid = Vec::new(); - let mut risk_grid: Vec<Vec<Option<i64>>> = Vec::new(); - for line in &input{ - let mut value_row = Vec::new(); - let mut risk_row = Vec::new(); - for c in line.chars() { - let value = c.to_digit(10).unwrap() as i64; - value_row.push(value); - risk_row.push(None); - } - value_grid.push(value_row); - risk_grid.push(risk_row); - } - - let height = value_grid.len(); - let width = value_grid[0].len(); - - /* - risk_grid[0][0] = Some(0); - find_path(0, 0, &value_grid, &mut risk_grid, false); - let risk = risk_grid[height - 1][width - 1].unwrap(); - println!("{}", risk); - */ - - let mut big_risk_grid = Vec::new(); - for _ in 0 .. height * 5 { - let mut big_risk_grid_row = Vec::new(); - for _ in 0 .. width * 5 { - big_risk_grid_row.push(None); - } - big_risk_grid.push(big_risk_grid_row); - } - big_risk_grid[0][0] = Some(0); - find_path(0, 0, &value_grid, &mut big_risk_grid, true); - let big_risk = big_risk_grid[height * 5 - 1][width * 5 - 1].unwrap(); - println!("{}", big_risk); - - Ok(()) -} - - -#[allow(dead_code)] -fn debug_risk_grid(risk_grid: &Vec<Vec<Option<i64>>>) { - for row in risk_grid { - for cell in row { - match cell { - None => { print!(" ."); } - Some(n) => { print!(" {}", n); } - } - } - println!(""); - } - println!(""); -} - - -fn find_path(x: usize, y: usize, value_grid: &Vec<Vec<i64>>, - risk_grid: &mut Vec<Vec<Option<i64>>>, repeat: bool) -{ - let mut height = value_grid.len(); - let mut width = value_grid[0].len(); - if repeat { - width *= 5; - height *= 5; - } - - let mut exploration_stack = Vec::new(); - exploration_stack.push((x, y)); - - let mut counter = 0; - - loop { - let (x, y) = match exploration_stack.pop() { - None => { return; } - Some(value) => { value } - }; - let risk_so_far = risk_grid[y][x].unwrap(); - counter += 1; - if counter % 100000 == 0 { - print!(" [{},{} {}]", x, y, risk_so_far); - } - - if x > 0 { - let value_left = get_value(x - 1, y, value_grid); - let new_risk_left = risk_so_far + value_left; - - let risk_left = risk_grid[y][x-1]; - let should_move = match risk_left { - None => true, - Some(prior_best_risk) => { - new_risk_left < prior_best_risk - } - }; - if should_move { - risk_grid[y][x-1] = Some(new_risk_left); - - exploration_stack.push((x - 1, y)); - } - } - - if y > 0 { - let value_up = get_value(x, y - 1, value_grid); - let new_risk_up = risk_so_far + value_up; - - let risk_up = risk_grid[y-1][x]; - let should_move = match risk_up { - None => true, - Some(prior_best_risk) => { - new_risk_up < prior_best_risk - } - }; - if should_move { - risk_grid[y-1][x] = Some(new_risk_up); - - exploration_stack.push((x, y - 1)); - } - } - - if x + 1 < width { - let value_right = get_value(x + 1, y, value_grid); - let new_risk_right = risk_so_far + value_right; - - let risk_right = risk_grid[y][x+1]; - let should_move = match risk_right { - None => true, - Some(prior_best_risk) => { - new_risk_right < prior_best_risk - } - }; - if should_move { - risk_grid[y][x+1] = Some(new_risk_right); - - exploration_stack.push((x + 1, y)); - } - } - - if y + 1 < height { - let value_down = get_value(x, y + 1, value_grid); - let new_risk_down = risk_so_far + value_down; - - let risk_down = risk_grid[y+1][x]; - let should_move = match risk_down { - None => true, - Some(prior_best_risk) => { - new_risk_down < prior_best_risk - } - }; - if should_move { - risk_grid[y+1][x] = Some(new_risk_down); - - exploration_stack.push((x, y + 1)); - } - } - } -} - - -fn get_value(x: usize, y: usize, value_grid: &Vec<Vec<i64>>) -> i64 { - let height = value_grid.len(); - let width = value_grid[0].len(); - - let tile_x = x / width; - let tile_y = y / height; - let increment: i64 = (tile_x + tile_y) as i64; - let raw_value = value_grid[y % height][x % width]; - (raw_value + increment - 1) % 9 + 1 -} - diff --git a/15/tests/main.rs b/15/tests/main.rs index 3e205b0..09dddae 100644 --- a/15/tests/main.rs +++ b/15/tests/main.rs @@ -5,7 +5,7 @@ use std::process::Command; #[test] fn personal_input() -> Result<(), Box<dyn std::error::Error>> { - let mut command = Command::cargo_bin("advent_14")?; + let mut command = Command::cargo_bin("advent_15")?; command.arg("input"); command.assert().success().stdout( |