[rust][day_02] Simplification

wip
Alpha Chen 8 years ago
parent d9c69654e9
commit ece6b3041d

@ -1,7 +1,5 @@
pub fn solve(input: &str) -> i32 { pub fn solve(input: &str) -> i32 {
let presents = input.lines().map(|line| Present::new(line).ribbon()).sum::<u32>() as i32
input.split('\n').map(|line| Present::new(line)).collect::<Vec<Present>>();
presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32
} }
struct Present { struct Present {
@ -15,7 +13,7 @@ impl Present {
fn new(input: &str) -> Self { fn new(input: &str) -> Self {
let dimensions = input.split('x') let dimensions = input.split('x')
.map(|d| d.parse::<u32>().unwrap()) .map(|d| d.parse::<u32>().unwrap())
.collect::<Vec<u32>>(); .collect::<Vec<_>>();
Present { Present {
length: dimensions[0], length: dimensions[0],
width: dimensions[1], width: dimensions[1],
@ -32,7 +30,7 @@ impl Present {
} }
fn surface_area(&self) -> u32 { fn surface_area(&self) -> u32 {
self.side_areas().iter().fold(0, |acc, &area| acc + 2 * area) self.side_areas().iter().map(|area| 2 * area).sum()
} }
fn slack(&self) -> u32 { fn slack(&self) -> u32 {

Loading…
Cancel
Save