From 8d26a5df6aab700c304d4c744387ab8de2590a26 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 2 Dec 2017 12:52:16 -0800 Subject: [PATCH] [2017][rust][2.x] --- 2017/rust/src/day_01.rs | 1 + 2017/rust/src/day_02.rs | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/2017/rust/src/day_01.rs b/2017/rust/src/day_01.rs index 61971a6..fdd0cd2 100644 --- a/2017/rust/src/day_01.rs +++ b/2017/rust/src/day_01.rs @@ -1,5 +1,6 @@ use failure::*; +#[allow(dead_code)] pub fn solve(input: &str) -> Result { let input: Vec = input .trim() diff --git a/2017/rust/src/day_02.rs b/2017/rust/src/day_02.rs index 2eafa08..f8d495a 100644 --- a/2017/rust/src/day_02.rs +++ b/2017/rust/src/day_02.rs @@ -4,16 +4,20 @@ pub fn solve(input: &str) -> Result { Ok( input .trim() - .split("\n") + .split('\n') .map(|row| { - let row: Vec<_> = row.split("\t") + row.split('\t') .map(|x| x.parse::().unwrap()) - .collect(); - let min = row.iter().min().unwrap(); - let max = row.iter().max().unwrap(); - max - min + .collect::>() }) + .map(|row| checksum(&row)) .sum::() .to_string(), ) } + +fn checksum(row: &[usize]) -> usize { + let min = row.iter().min().unwrap(); + let max = row.iter().max().unwrap(); + max - min +}