2022-11-24 — 2 min read

Advent of Code 2015, Day 2 – In Ruby

Let’s see the puzzle for the second day of 2015.

The first half of the puzzle

The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.

Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2lw + 2wh + 2hl. The elves also need a little extra paper for each present: the area of the smallest side.

For example:

  • A present with dimensions 2x3x4 requires 26 + 212 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet.
  • A present with dimensions 1x1x10 requires 21 + 210 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.

All numbers in the elves’ list are in feet. How many total square feet of wrapping paper should they order?

The input contains a bunch of box sizes (similar to 2x3x4), each on its own line.

The solution:

#!/usr/bin/env ruby

f = File.read(ARGV.first)
area = 0

f.each_line do |line|
  x, y, z = line.split('x').map(&:to_i).sort
  area += 2 * (x * y + x * z + y * z) + (x * y)
end

puts "Wrapping paper needed: #{area}."

First, we read the input file. We’ll also need a variable to store the accumulated amount of wrapping paper (area). Then we iterate over the input line-by-line, for each line, we separate the string into its three components (width, height, length), using x as a separator. Since the three parts are still strings, we convert them to integers, then sort them. The sorting is necessary because this way the smallest side (which is the extra paper we need to add) will always be x*y. Lastly, we add the required wrapping paper for this gift to the total.

The second half of the puzzle

The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact.

The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don’t ask how they tie the bow, though; they’ll never tell.

For example:

  • A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 234 = 24 feet of ribbon for the bow, for a total of 34 feet.
  • A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1110 = 10 feet of ribbon for the bow, for a total of 14 feet.

How many total feet of ribbon should they order?

The solution:

#!/usr/bin/env ruby

f = File.read(ARGV.first)
length = 0

f.each_line do |line|
  x, y, z = line.split('x').map(&:to_i).sort
  length += 2 * (x + y) + (x * y * z)
end

puts "Ribbon needed: #{length}."

The solution is similar to the first part: read the file, split, convert and sort the dimensions, then add the calculated length to the total.

This is it for day 2, looking forward to day 3!

The code with my input text is available in the GitHub repo.

Thanks for reading! If you have any comments, additions, or corrections, feel free to reach me via e-mail.

Copyright © 2023 csm.hu
Contact