Skip to content

Instantly share code, notes, and snippets.

@kaibrabo
Forked from flagoworld/diagonal_difference.rb
Last active October 16, 2017 05:00
Show Gist options
  • Save kaibrabo/941fd333b0796f0075e0a92fc279996d to your computer and use it in GitHub Desktop.
Save kaibrabo/941fd333b0796f0075e0a92fc279996d to your computer and use it in GitHub Desktop.
n = gets.strip.to_i
a = Array.new(n)
for a_i in (0..n-1)
a_t = gets.strip
a[a_i] = a_t.split(' ').map(&:to_i)
arr = a[a_i]
end
a_diagonal = (0...arr.length).collect { |h|
a[h][h]
}.reduce(:+)
rev_a = a.each { |i|
i.reverse!
}
b_diagonal = (0...arr.length).collect { |i|
rev_a[i][i]
}.reduce(:+)
p (a_diagonal - b_diagonal).abs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment