-
-
Save JohnTheodore/b661a45d619d8e2da41a 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/ruby | |
require 'debugger' | |
# Box Stacking | |
class BoxStacking | |
attr_accessor :file, :boxen_hash, :total_boxes, :sorted_base_area, :stack_options | |
def initialize(input) | |
@file = File.open(input).map(&:strip) | |
@total_boxes = @file.shift.strip.to_i | |
@prefix_size = @total_boxes.to_s.size | |
@boxen_hash = {} | |
populate_boxen_hash(get_file_integers(@file)) | |
@stack_options = [] | |
@sorted_base_area = get_sorted_base_area(@boxen_hash) | |
set_stackables | |
populate_stack_options(@boxen_hash) | |
end | |
def get_box_orientations(dimensions) | |
dimensions.permutation.group_by(&:last).values.map(&:first) | |
end | |
def get_file_integers(file_strings) | |
file_strings.map { |str| str.split(" ").map(&:to_i) } | |
end | |
def get_box_stats(base_id, dimensions) | |
box_stats_hash = { } | |
id = base_id.to_s.rjust(@total_boxes.to_s.size, "0") | |
get_box_orientations(dimensions).each_with_index do |orientation, index| | |
length, width = orientation[0], orientation[1] | |
stats = { "dimensions" => orientation, "base_area" => length*width } | |
box_stats_hash["box#{id.to_s}#{index + 1}"] = stats | |
end | |
box_stats_hash | |
end | |
def get_sorted_base_area(master_hash) | |
base_area_hash = {} | |
master_hash.each do |key, value| | |
base_area_hash[key] = master_hash[key]['base_area'] | |
end | |
base_area_hash.sort_by {|key, value| value } | |
end | |
def get_lw(box) | |
@boxen_hash[box]['dimensions'][(0..1)].sort | |
end | |
def populate_boxen_hash(box_ints) | |
box_ints.each_with_index do |box_dimensions, index| | |
@boxen_hash = @boxen_hash.merge(get_box_stats(index, box_dimensions)) | |
end | |
end | |
def populate_stack_options(boxen) | |
tmp = [] | |
puts boxen | |
end | |
def box_prefix_match?(box_a, box_b) | |
box_a[(0..(2+@prefix_size.to_s.size))] == box_b[(0..(2+@prefix_size.to_s.size))] | |
end | |
def stackable?(btm_box, top_box) | |
btmboxlw, topboxlw = get_lw(btm_box[0]), get_lw(top_box[0]) | |
(btmboxlw[0] >= topboxlw[0]) && (btmboxlw[1] >= topboxlw[1]) && !box_prefix_match?(btm_box[0], top_box[0]) | |
end | |
def set_stackables | |
@sorted_base_area[(0..-1)].each_with_index do |btmbox, index| | |
stackables = [] | |
@sorted_base_area[(0..(index + 1))].each do |topbox| | |
stackables << topbox[0] if stackable?(btmbox, topbox) | |
end | |
@boxen_hash[btmbox[0]]['stackables'] = stackables | |
end | |
end | |
end | |
start_time = Time.now | |
stacking = BoxStacking.new(ARGV[0]) | |
#puts "#{stacking.boxen_hash}" | |
puts "Benchmarktime: #{Time.now - start_time}" | |
# do some DFS stuff with @boxen_hash | |
# add something where if it hits the theoretical max height, it stops and declares victory |
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
3 | |
5 2 4 | |
1 4 2 | |
4 4 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment