[2016][rust][3.0]

profile
Alpha Chen 8 years ago
parent 2b7b747ca5
commit d93aff516a

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
pub fn solve(input: &str) -> String {
let parsed = input.split_whitespace()
.map(str::parse)
.collect::<Result<Vec<usize>, _>>()
.expect("unable to parse input");
let count = parsed.chunks(3)
.map(|w| Triangle(w[0], w[1], w[2]))
.filter(Triangle::is_valid)
.count();
count.to_string()
}
struct Triangle(usize, usize, usize);
impl Triangle {
fn is_valid(&self) -> bool {
self.0 + self.1 > self.2 && self.0 + self.2 > self.1 &&
self.1 + self.2 > self.0
}
}
#[test]
fn test_triangle() {
assert!(!Triangle(5, 10, 25).is_valid());
}

@ -1,2 +1,3 @@
pub mod day_01; pub mod day_01;
pub mod day_02; pub mod day_02;
pub mod day_03;

@ -8,5 +8,5 @@ fn main() {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_to_string(&mut input).ok(); io::stdin().read_to_string(&mut input).ok();
println!("{}", day_02::solve(&input)); println!("{}", day_03::solve(&input));
} }

Loading…
Cancel
Save