Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created November 19, 2011 15:39
Show Gist options
  • Save qoobaa/1378971 to your computer and use it in GitHub Desktop.
Save qoobaa/1378971 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
pictures = ARGV.map(&:to_i).sort
desired_group_size = ENV["GROUP_SIZE"].to_i
min_distance, max_distance = pictures.each_cons(2).map { |a, b| b - a }.minmax
groups = pictures.map { |picture| [picture] }
# Calculates distance factor using linear function.
#
# 1.0 when distance == min_distance
# ...
# 0.0 when distance == max_distance
def distance_factor(distance, min_distance, max_distance)
minmax_distance = (max_distance - min_distance).to_f
-distance / minmax_distance + max_distance / minmax_distance
end
# Calculates group size factor.
#
# 1.0 if the group_size does not exceed desired_group_size
# 0.5 if the group_size == desired_group_size + 1
# 0.25 if the group_size == desired_group_size + 2
# ...
def group_size_factor(group_size, desired_group_size)
if group_size <= desired_group_size
1.0
else
2.0 ** (desired_group_size - group_size)
end
end
groups.each_cons(2) do |first, second|
distance = second.first - first.last
group_size = first.size + second.size
cost = 1
cost *= distance_factor(distance, min_distance, max_distance)
cost *= group_size_factor(group_size, desired_group_size)
if cost > rand
# move content of the first group to the second group
second.unshift(*first.shift(first.size))
end
end
groups.delete_if(&:empty?)
puts groups.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment