use crate::Solution;
use std::cmp::min;
problem!(Problem0081, 81, "Path Sum: Two Ways");
impl Solution for Problem0081 {
fn solve(&self) -> String {
const INPUT: &str = include_str!("p081_matrix.txt");
let mut matrix = INPUT
.trim()
.lines()
.map(|line| {
line.trim()
.split(',')
.map(|num_str| num_str.parse::<u32>().unwrap())
.collect::<Vec<u32>>()
})
.collect::<Vec<Vec<u32>>>();
let last_row = matrix.len() - 1;
for i in (0..(matrix[last_row].len() - 1)).rev() {
matrix[last_row][i] += matrix[last_row][i + 1];
}
let last_col = matrix[0].len() - 1;
for i in (0..(matrix.len() - 1)).rev() {
matrix[i][last_col] += matrix[i + 1][last_col];
}
for i in (0..(matrix.len() - 1)).rev() {
for j in (0..(matrix[0].len() - 1)).rev() {
matrix[i][j] += min(matrix[i][j + 1], matrix[i + 1][j]);
}
}
matrix[0][0].to_string()
}
}