-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_sum_two_ways.rs
More file actions
27 lines (24 loc) · 847 Bytes
/
path_sum_two_ways.rs
File metadata and controls
27 lines (24 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::io::BufRead;
pub fn solve() -> i64 {
let fhandle = std::fs::File::open("res/solutions/path_sum_two_ways.txt").unwrap();
let reader = std::io::BufReader::new(fhandle);
let mut matrix: Vec<Vec<i32>> = reader
.lines()
.map(|line| line.unwrap().split(',').map(|s| s.parse().unwrap()).collect())
.collect();
// Bottom-up dynamic programming.
for row in (0..80).rev().skip(1) {
matrix[row][79] += matrix[row + 1][79];
}
for col in (0..80).rev().skip(1) {
matrix[79][col] += matrix[79][col + 1];
}
for row in (0..80).rev().skip(1) {
for col in (0..80).rev().skip(1) {
matrix[row][col] += std::cmp::min(matrix[row + 1][col], matrix[row][col + 1]);
}
}
let result = matrix[0][0];
assert_eq!(result, 427337);
result as i64
}