You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2015/rust/src/day_02.rs

74 lines
1.6 KiB

pub fn solve(input: &str) -> i32 {
input.lines().map(|line| Present::new(line).ribbon()).sum::<u32>() as i32
9 years ago
}
struct Present {
length: u32,
width: u32,
height: u32,
9 years ago
}
#[allow(dead_code)]
9 years ago
impl Present {
fn new(input: &str) -> Self {
let dimensions = input.split('x')
.map(|d| d.parse::<u32>().unwrap())
.collect::<Vec<_>>();
Present {
length: dimensions[0],
width: dimensions[1],
height: dimensions[2],
9 years ago
}
}
9 years ago
fn wrapping_paper(&self) -> u32 {
self.surface_area() + self.slack()
}
9 years ago
fn ribbon(&self) -> u32 {
*self.perimeters().iter().min().unwrap_or(&0) + self.volume()
}
9 years ago
fn surface_area(&self) -> u32 {
self.side_areas().iter().map(|area| 2 * area).sum()
}
9 years ago
fn slack(&self) -> u32 {
*self.side_areas().iter().min().unwrap_or(&0)
}
9 years ago
fn perimeters(&self) -> Vec<u32> {
vec![self.length + self.width,
self.width + self.height,
self.height + self.length]
.iter()
.map(|&x| 2 * x)
.collect()
}
9 years ago
fn volume(&self) -> u32 {
self.length * self.width * self.height
}
9 years ago
fn side_areas(&self) -> Vec<u32> {
vec![self.length * self.width,
self.width * self.height,
self.height * self.length]
}
9 years ago
}
#[test]
fn test_day02() {
let mut present = Present::new("2x3x4");
assert_eq!(52, present.surface_area());
assert_eq!(6, present.slack());
assert_eq!(58, present.wrapping_paper());
assert_eq!(34, present.ribbon());
9 years ago
present = Present::new("1x1x10");
assert_eq!(42, present.surface_area());
assert_eq!(1, present.slack());
assert_eq!(43, present.wrapping_paper());
assert_eq!(14, present.ribbon());
9 years ago
}