You are viewing a single comment's thread. Return to all comments →
Rust solution
fn formingMagicSquare(s: &[Vec<i32>]) -> i32 { let magic_squares = [ vec![6, 1, 8, 7, 5, 3, 2, 9, 4], vec![8, 1, 6, 3, 5, 7, 4, 9, 2], vec![6, 7, 2, 1, 5, 9, 8, 3, 4], vec![8, 3, 4, 1, 5, 9, 6, 7, 2], vec![2, 7, 6, 9, 5, 1, 4, 3, 8], vec![4, 3, 8, 9, 5, 1, 2, 7, 6], vec![2, 9, 4, 7, 5, 3, 6, 1, 8], vec![4, 9, 2, 3, 5, 7, 8, 1, 6], ]; let mut min_cost = i32::max_value(); for magic in magic_squares { let curr_cost = s .iter() .enumerate() .fold(0, |mut acc, (row_id, row_ar)| { for n in 0..3 { acc += (row_ar[n] - magic[row_id * 3 + n]).abs() } acc }); min_cost = std::cmp::min(min_cost, curr_cost) } min_cost }
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
Rust solution