Created
November 29, 2022 23:50
-
-
Save copiousfreetime/2609b03f49945dbef278d019bf2af62c 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
#!/usr/bin/env ruby | |
# Run this program on the commandline as: | |
# | |
# ruby day-1.rb <filename of csv> | |
# | |
# The output will be a prettified JSON representation of the result | |
# | |
require 'csv' | |
require 'json' | |
KILOBYTES_PER_MEGABYTE = 1024 | |
BUCKET_LIMIT = 120 * KILOBYTES_PER_MEGABYTE | |
Track = Struct.new(:url, :email, :track, :kilobytes) | |
# | |
# load up all the tracks from the file by their url | |
# | |
def load_data(path:) | |
loaded_data = Hash.new { |h,k| h[k] = Array.new } | |
CSV.foreach(path, headers: :first_row, converters: :integer, return_headers: false) do |row| | |
track = Track.new(*row.fields) | |
loaded_data[track.url] << track | |
end | |
return loaded_data | |
end | |
# | |
# bucket a list of tracks into groups where the sum of hte kilobytes in the | |
# group cannot exceed the bucket limit | |
# | |
def bucket_tracks(tracks: , bucket_limit: BUCKET_LIMIT) | |
buckets = [] | |
current_bucket = [] | |
current_kb = 0 | |
tracks.each do |track| | |
if (current_kb + track.kilobytes) > bucket_limit then | |
buckets << current_bucket | |
puts "current_kb: #{current_kb} - #{track.kilobytes}" | |
current_bucket = [] | |
current_kb = 0 | |
end | |
current_bucket << [track.track, track.kilobytes] | |
current_kb += track.kilobytes | |
end | |
buckets << current_bucket | |
return buckets | |
end | |
### | |
### | |
### | |
input_file = ARGV.shift | |
abort "Input file required" unless input_file | |
organized = {} | |
load_data(path: input_file).each do |url, tracks| | |
organized[url] = bucket_tracks(tracks: tracks) | |
end | |
puts JSON.pretty_generate(organized) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment