gets puts readlines.inject(0) { |sum, line| start_time, end_time = line.chomp.split start_hour, start_min = start_time.split(':').map(&:to_i) end_hour, end_min = end_time.split(':').map(&:to_i) diff_hour = end_hour - start_hour diff_hour = 24 + diff_hour if diff_hour.negative? diff_min = end_min - start_min if diff_min.negative? if diff_hour.zero? diff_hour = 23 else diff_hour -= 1 end diff_min = 60 + diff_min end sum + (diff_hour % 24) * 60 + diff_min }