From 514bb627bf7b71ec006e272b78c7e51eb92119fd Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 28 Dec 2015 08:27:44 -0800 Subject: [PATCH] Rust, Day 2.0 --- rust/src/day_02.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/rust/src/day_02.rs b/rust/src/day_02.rs index ef3aac8..c24aa09 100644 --- a/rust/src/day_02.rs +++ b/rust/src/day_02.rs @@ -3,16 +3,17 @@ use std::io; use day::Day; pub struct Day02 { - input: String, + presents: Vec, } impl Day for Day02 { fn new(input: String) -> Day02 { - Day02 { input: input } + let presents = input.split("\n").map(|line| Present::new(line)); + Day02 { presents: presents.collect::>() } } fn solve(self) -> io::Result { - Ok(1) + Ok(self.presents.iter().fold(0u32, |acc, present| acc + present.wrapping_paper()) as i32) } } @@ -23,7 +24,7 @@ struct Present { } impl Present { - fn new(input: String) -> Self { + fn new(input: &str) -> Self { let dimensions = input.split("x") .map(|d| d.parse::().unwrap()) .collect::>(); @@ -49,12 +50,12 @@ impl Present { #[test] fn test_day02() { - let mut present = Present::new("2x3x4".to_string()); + let mut present = Present::new("2x3x4"); assert_eq!(52, present.surface_area()); assert_eq!(6, present.slack()); assert_eq!(58, present.wrapping_paper()); - present = Present::new("1x1x10".to_string()); + present = Present::new("1x1x10"); assert_eq!(42, present.surface_area()); assert_eq!(1, present.slack()); assert_eq!(43, present.wrapping_paper());