-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.rs
56 lines (48 loc) · 1.13 KB
/
day2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::{cmp::min, num::ParseIntError, str::FromStr};
use anyhow::Result;
#[derive(Debug)]
pub struct Crate {
l: usize,
w: usize,
h: usize,
}
impl FromStr for Crate {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let lwh: Vec<usize> = s.split('x').map(|c| c.parse()).collect::<Result<_, _>>()?;
Ok(Crate {
l: lwh[0],
w: lwh[1],
h: lwh[2],
})
}
}
impl Crate {
fn vol(&self) -> usize { self.l * self.w * self.h }
fn min_perim(&self) -> usize {
vec![
2 * self.l + 2 * self.w,
2 * self.h + 2 * self.w,
2 * self.h + 2 * self.l,
]
.into_iter()
.min()
.unwrap()
}
fn paper(&self) -> usize {
let a = 2 * self.l * self.w;
let b = 2 * self.w * self.h;
let c = 2 * self.h * self.l;
a + b + c + min(a, min(b, c)) / 2
}
}
pub fn day2() -> Result<(usize, usize)> {
let crates: Vec<Crate> = std::fs::read_to_string("../inputs/day2.txt")?
.lines()
.flat_map(|s| s.parse::<Crate>())
.collect();
Ok((
crates.iter().map(|s| s.paper()).sum(),
crates.iter().map(|s| s.vol() + s.min_perim()).sum(),
))
}