diff --git a/src/day17.rs b/src/day17.rs index a690066..a93195c 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -1,6 +1,6 @@ use std::io; -fn find_min_heat_loss(board: &Vec>) -> u64 { +fn find_min_heat_loss(board: &Vec>, min_move: usize, max_move: usize) -> u64 { let mut horiz_min: Vec> = Vec::new(); let mut vert_min: Vec> = Vec::new(); @@ -19,29 +19,28 @@ fn find_min_heat_loss(board: &Vec>) -> u64 { for r in 0..board.len() { for c in 0..board[r].len() { let prev = horiz_min[r][c]; + // Check Backwards. - if c > 0 && vert_min[r][c - 1] != u64::MAX { - horiz_min[r][c] = horiz_min[r][c].min(vert_min[r][c - 1] + board[r][c - 1]); - } - if c > 1 && vert_min[r][c - 2] != u64::MAX { - horiz_min[r][c] = - horiz_min[r][c].min(vert_min[r][c - 2] + board[r][c - 1] + board[r][c - 2]); - } - if c > 2 && vert_min[r][c - 3] != u64::MAX { - horiz_min[r][c] = horiz_min[r][c] - .min(vert_min[r][c - 3] + board[r][c - 1] + board[r][c - 2] + board[r][c - 3]); + let mut sum_back = 0; + let mut m = 1; + while m <= max_move && c >= m { + sum_back += board[r][c - m]; + if m >= min_move && vert_min[r][c - m] != u64::MAX { + horiz_min[r][c] = horiz_min[r][c].min(vert_min[r][c - m] + sum_back); + } + + m += 1; } + // Check Forwards. - if c < board[r].len() - 1 && vert_min[r][c + 1] != u64::MAX { - horiz_min[r][c] = horiz_min[r][c].min(vert_min[r][c + 1] + board[r][c + 1]); - } - if c < board[r].len() - 2 && vert_min[r][c + 2] != u64::MAX { - horiz_min[r][c] = - horiz_min[r][c].min(vert_min[r][c + 2] + board[r][c + 1] + board[r][c + 2]); - } - if c < board[r].len() - 3 && vert_min[r][c + 3] != u64::MAX { - horiz_min[r][c] = horiz_min[r][c] - .min(vert_min[r][c + 3] + board[r][c + 1] + board[r][c + 2] + board[r][c + 3]); + let mut sum_forwards = 0; + let mut m = 1; + while m <= max_move && c + m < board[r].len() { + sum_forwards += board[r][c + m]; + if m >= min_move && vert_min[r][c + m] != u64::MAX { + horiz_min[r][c] = horiz_min[r][c].min(vert_min[r][c + m] + sum_forwards); + } + m += 1; } if horiz_min[r][c] != prev { @@ -54,30 +53,28 @@ fn find_min_heat_loss(board: &Vec>) -> u64 { for r in 0..board.len() { for c in 0..board[r].len() { let prev = vert_min[r][c]; - // Check Backwards - if r > 0 && horiz_min[r - 1][c] != u64::MAX { - vert_min[r][c] = vert_min[r][c].min(horiz_min[r - 1][c] + board[r - 1][c]); - } - if r > 1 && horiz_min[r - 2][c] != u64::MAX { - vert_min[r][c] = - vert_min[r][c].min(horiz_min[r - 2][c] + board[r - 1][c] + board[r - 2][c]); - } - if r > 2 && horiz_min[r - 3][c] != u64::MAX { - vert_min[r][c] = vert_min[r][c] - .min(horiz_min[r - 3][c] + board[r - 1][c] + board[r - 2][c] + board[r - 3][c]); + + // Check Backwards. + let mut sum_back = 0; + let mut m = 1; + while m <= max_move && r >= m { + sum_back += board[r - m][c]; + if m >= min_move && horiz_min[r - m][c] != u64::MAX { + vert_min[r][c] = vert_min[r][c].min(horiz_min[r - m][c] + sum_back); + } + + m += 1; } // Check Forwards. - if r < board.len() - 1 && horiz_min[r + 1][c] != u64::MAX { - vert_min[r][c] = vert_min[r][c].min(horiz_min[r + 1][c] + board[r + 1][c]); - } - if r < board.len() - 2 && horiz_min[r + 2][c] != u64::MAX { - vert_min[r][c] = - vert_min[r][c].min(horiz_min[r + 2][c] + board[r + 1][c] + board[r + 2][c]); - } - if r < board.len() - 3 && horiz_min[r + 3][c] != u64::MAX { - vert_min[r][c] = vert_min[r][c] - .min(horiz_min[r + 3][c] + board[r + 1][c] + board[r + 2][c] + board[r + 3][c]); + let mut sum_forwards = 0; + let mut m = 1; + while m <= max_move && r + m < board.len() { + sum_forwards += board[r + m][c]; + if m >= min_move && horiz_min[r + m][c] != u64::MAX { + vert_min[r][c] = vert_min[r][c].min(horiz_min[r + m][c] + sum_forwards); + } + m += 1; } if vert_min[r][c] != prev { @@ -94,7 +91,7 @@ fn find_min_heat_loss(board: &Vec>) -> u64 { horiz_min[0][0].min(vert_min[0][0]) } -pub fn min_heat_loss() -> u64 { +pub fn min_heat_loss(min_move: usize, max_move: usize) -> u64 { let mut buffer = String::new(); while io::stdin().read_line(&mut buffer).unwrap() > 0 {} @@ -105,5 +102,5 @@ pub fn min_heat_loss() -> u64 { .map(|s| s.chars().map(|c| (c as u64 - '0' as u64)).collect()) .collect(); - find_min_heat_loss(&board) + find_min_heat_loss(&board, min_move, max_move) } diff --git a/src/main.rs b/src/main.rs index 39acb04..5b67824 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,8 @@ fn main() { "day15b" => println!("Sum: {}", day15::hash_map()), "day16a" => println!("Answer: {}", day16::solve()), "day16b" => println!("Answer: {}", day16::find_best()), - "day17a" => println!("Minimum: {}", day17::min_heat_loss()), + "day17a" => println!("Minimum: {}", day17::min_heat_loss(1, 3)), + "day17b" => println!("Minimum: {}", day17::min_heat_loss(4, 10)), _ => println!("Unrecognized day: {}", args[1]), } }