Skip to content

Instantly share code, notes, and snippets.

@wconrad
Last active December 6, 2015 12:51
Show Gist options
  • Save wconrad/d7b88797e49b7a3a7229 to your computer and use it in GitHub Desktop.
Save wconrad/d7b88797e49b7a3a7229 to your computer and use it in GitHub Desktop.
Advent of code, day 2
#!/usr/bin/env ruby
# Advent of code, day 2
# http://adventofcode.com/day/2
class Box
def self.from_input(line)
dims = line.split("x").map do |s|
Integer(s)
end
new(dims)
end
def initialize(dims)
@dims = dims
end
def area
2 * side_areas.reduce(&:+)
end
def area_of_smallest_side
side_areas.min
end
def wrapping_paper_area
area + area_of_smallest_side
end
private
def side_areas
@dims.combination(2).map do |x, y|
x * y
end
end
end
total_wrapping_paper_area = File.read("input").lines.map do |line|
Box.from_input(line).wrapping_paper_area
end.reduce(&:+)
puts total_wrapping_paper_area
#!/usr/bin/env ruby
# Advent of code, day 2
# http://adventofcode.com/day/2
class Box
def self.from_input(line)
dims = line.split("x").map do |s|
Integer(s)
end
new(dims)
end
def initialize(dims)
@dims = dims
end
def feet_of_ribbon
smallest_perimeter + volume
end
def wrapping_paper_area
area + area_of_smallest_side
end
private
def area
2 * side_areas.reduce(&:+)
end
def area_of_smallest_side
side_areas.min
end
def volume
@dims.reduce(&:*)
end
def smallest_perimeter
perimeters.min
end
def side_areas
@dims.combination(2).map do |x, y|
x * y
end
end
def perimeters
@dims.combination(2).map do |x, y|
2 * (x + y)
end
end
end
boxes = File.read("input").lines.map do |line|
Box.from_input(line)
end
total_wrapping_paper_area = boxes.map(&:wrapping_paper_area).reduce(&:+)
total_feet_of_ribbon = boxes.map(&:feet_of_ribbon).reduce(&:+)
puts total_wrapping_paper_area
puts total_feet_of_ribbon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment