Last active
December 5, 2015 20:56
-
-
Save boone/27ee38426e785f7ab8b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# http://adventofcode.com/day/2 - part 1 | |
# | |
# present_dimensions - String a list of present dimensions, each in the format of | |
# 1x2x3 where 1 is the length, 2 is the width and 3 is the height, | |
# in feet. | |
# | |
# Returns an integer of the area of wrapping paper required, in square feet. | |
def required_wrapping_paper(present_dimensions) | |
total_area = 0 | |
presents = present_dimensions.split("\n") | |
presents.each do |present| | |
length, width, height = present.split("x", 3).map(&:to_i) | |
total_area += paper_for_one_gift(length, width, height) | |
end | |
total_area | |
end | |
# http://adventofcode.com/day/2 - part 2 | |
# | |
# present_dimensions - String a list of present dimensions, each in the format of | |
# 1x2x3 where 1 is the length, 2 is the width and 3 is the height, | |
# in feet. | |
# | |
# Returns an integer of the length of wrapping paper required, in feet. | |
def required_ribbon(present_dimensions) | |
total_length = 0 | |
presents = present_dimensions.split("\n") | |
presents.each do |present| | |
length, width, height = present.split("x", 3).map(&:to_i) | |
total_length += ribbon_for_one_gift(length, width, height) | |
end | |
total_length | |
end | |
# determines the area of wrapping paper needed for one gift. | |
# | |
# length - Integer length, in feet. | |
# width - Integer width, in feet. | |
# height - Integer height, in feet. | |
# | |
# Returns an Integer in square feet. | |
def paper_for_one_gift(length, width, height) | |
side1 = length * width | |
side2 = width * height | |
side3 = length * height | |
2 * (side1 + side2 + side3) + [side1, side2, side3].min | |
end | |
# determines the length of ribbon needed for one gift. | |
# | |
# length - Integer length, in feet. | |
# width - Integer width, in feet. | |
# height - Integer height, in feet. | |
# | |
# Returns an Integer in feet. | |
def ribbon_for_one_gift(length, width, height) | |
sizes = [length, width, height] | |
bow_length = sizes.inject(:*) | |
# drop the longest value, since the ribbon uses the shorest perimeter | |
2 * ((sizes.sort[0, 2]).inject(:+)) + bow_length | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment