Last active
May 6, 2022 17:56
-
-
Save williamromero/516719b93b30ed8bbb057702f13d5669 to your computer and use it in GitHub Desktop.
Hackerank :: Minimum moves two arrays of integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def minimum_moves(*arrays) | |
return raise 'Length of arrays are not equal' if arrays.first.length != arrays.last.length | |
moves = 0 | |
slice_number = proc { |n| n.to_s.chars.map(&:to_i) } | |
arrays.length.times do |i| | |
first = slice_number.(arrays.first[i]) | |
second = slice_number.(arrays.last[i]) | |
first.map.with_index { |n, i| moves += (first[i] < second[i]) ? (second[i] - first[i]) : (first[i] - second[i]) unless (first[i] == second[i]) && (first.length != second.length) } | |
end | |
return moves | |
end | |
p minimum_moves([1234, 4321],[2345,3214]) # 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment