Skip to content

Instantly share code, notes, and snippets.

@amosrivera
Created February 26, 2016 20:16
Show Gist options
  • Select an option

  • Save amosrivera/21fbaf2b59e05e65aa06 to your computer and use it in GitHub Desktop.

Select an option

Save amosrivera/21fbaf2b59e05e65aa06 to your computer and use it in GitHub Desktop.
require 'pry'
file = File.new(ARGV[0], "r")
# delete current output file
if File.exist?("output.txt")
File.delete("output.txt")
end
# number of cases
cases = file.gets
cases.to_i.times do |x|
intervals = file.gets
counts = file.gets.split.map(&:to_i)
temp = counts
first = temp.shift
prev = first
total = 0
counts.each do |n|
if n < prev
total += (n - prev).abs
end
prev = n
end
# method 2
difference = 0
total_2 = 0
prev = first
temp.each do |n|
if prev > n && (prev - n) > difference
difference = prev - n
end
prev = n
end
prev = first
temp.each do |n|
if(prev < difference)
total_2 += prev
elsif
total_2 += difference
end
prev = n
end
case_n = x+1
File.open("output.txt","a") do |output|
output.puts "Case ##{case_n}: #{total} #{total_2}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment