More minitest

This commit is contained in:
Bill Rossi 2025-11-30 21:21:41 -05:00
parent cddfabe954
commit f08a53f5e5
3 changed files with 62 additions and 0 deletions

7
ruby/2015/2/bin/problem Normal file
View File

@ -0,0 +1,7 @@
#/usr/bin/env ruby
require "present"
lines = STDIN.read.chomp.split("\n")
presents = lines.map { |line| Present.for line }
puts "Part 1: #{presents.map(&:paper).sum}"
puts "Part 2: #{presents.map(&:ribbon).sum}"

View File

@ -0,0 +1,41 @@
class Present
attr_reader :l, :w, :h
def initialize(l, w, h)
@l = l
@w = w
@h = h
end
def self.for(string)
new *string.split("x").map(&:to_i)
end
def paper
surface_area + slack
end
def side_areas
[l*w, l*h, w*h]
end
def surface_area
side_areas.sum * 2
end
def slack
side_areas.min
end
def perimeters
[2*(l+w), 2*(l+h), 2*(w+h)]
end
def volume
l*w*h
end
def ribbon
perimeters.min + volume
end
end

View File

@ -0,0 +1,14 @@
require "minitest/autorun"
require "present"
class TestPresent < Minitest::Test
def test_paper
assert_equal(58, Present.for("2x3x4").paper)
assert_equal(43, Present.for("1x1x10").paper)
end
def test_ribbon
assert_equal(34, Present.for("2x3x4").ribbon)
assert_equal(14, Present.for("1x1x10").ribbon)
end
end