Created
July 1, 2020 06:15
-
-
Save trico/861d86bfa825f26b0c55b2a9ab37677f to your computer and use it in GitHub Desktop.
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
require 'csv' | |
require 'date' | |
memory_customer_id = 0 | |
memory_points = 0 | |
memory_last_date = nil | |
memory_location_id = 0 | |
debug = true | |
CSV.foreach('main.csv').each do |line| | |
customer_id = line[0] | |
date = line[1] | |
location_id = line[2] | |
if memory_customer_id != customer_id | |
puts "#{memory_customer_id},#{memory_points}" | |
memory_points = 0 | |
memory_customer_id = 0 | |
memory_last_date = nil | |
memory_location_id = 0 | |
end | |
if memory_location_id != location_id | |
memory_last_date = date | |
memory_location_id = location_id | |
memory_customer_id = customer_id | |
memory_location_id = location_id | |
memory_points += 1 | |
puts "pre-a #{date} - #{memory_points}" if debug | |
next | |
end | |
if memory_last_date == nil | |
memory_points = 1 | |
memory_last_date = date | |
memory_customer_id = customer_id | |
memory_location_id = location_id | |
puts "a #{date} - #{memory_points}" if debug | |
next | |
end | |
current_day = DateTime.strptime(date, '%Y-%m-%d %H:%M').day | |
memory_day = DateTime.strptime(memory_last_date, '%Y-%m-%d %H:%M').day | |
current_hour = DateTime.strptime(date, '%Y-%m-%d %H:%M').hour | |
memory_hour = DateTime.strptime(memory_last_date, '%Y-%m-%d %H:%M').hour | |
# when the asistence croses midnight | |
if memory_day != current_day | |
# last date is a day before current date | |
if (memory_day+1) == current_day | |
if current_hour >= 0 && current_hour <= 7 && memory_hour >= 9 | |
# no point must be won | |
memory_points += 0 | |
else | |
memory_points += 1 | |
end | |
else | |
memory_points += 1 | |
end | |
memory_last_date = date | |
memory_customer_id = customer_id | |
puts "b #{date} - #{memory_points}" if debug | |
next | |
end | |
if memory_day == current_day | |
if memory_hour >= 0 && memory_hour <= 7 && current_hour >= 0 && current_hour <= 7 | |
memory_points += 0 | |
memory_last_date = date | |
memory_customer_id = customer_id | |
puts "c #{date} - #{memory_points}" if debug | |
next | |
end | |
if memory_hour >= 0 && memory_hour <= 7 && current_hour >= 9 | |
memory_points += 1 | |
memory_last_date = date | |
memory_customer_id = customer_id | |
puts "d #{date} - #{memory_points}" if debug | |
next | |
end | |
end | |
end | |
puts "#{memory_customer_id},#{memory_points}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment