triangles.rs
View source code here on GitHub!
Includes
- pub fn triangle::reduce_triangle(triangle: Vec<Vec<u8>>) -> u16
Returns the highest value path in a given triangle-shaped vector.
1use std::cmp::max;
2
3pub fn reduce_triangle(triangle: Vec<Vec<u8>>) -> u16 {
4 let centering = triangle
5 .last()
6 .expect("Given vector must have at least one element")
7 .len() + 1;
8 let mut potential_totals: Vec<u16> = vec![0; centering];
9 for parent in triangle.iter().rev() {
10 for i in 0..(parent.len()) {
11 potential_totals[i] = max(potential_totals[i], potential_totals[i + 1]) + parent[i] as u16;
12 }
13 }
14 return potential_totals[0];
15}