From 07acfa0b030cc2f481f76fc85a664bfbcddbc623 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 6 Dec 2020 01:15:02 -0800 Subject: Add a function that trims the lines --- 06/src/main.rs | 3 ++- lib/src/lib.rs | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/06/src/main.rs b/06/src/main.rs index 72e4f1d..00d065c 100644 --- a/06/src/main.rs +++ b/06/src/main.rs @@ -12,7 +12,8 @@ fn main() -> Result<()> { let filename = args.next().unwrap(); let input = advent_lib::read_lines_file(&filename)?; - let groups = advent_lib::group_lines_by_blanks(input)?; + let trimmed = advent_lib::trim_lines(&input); + let groups = advent_lib::group_lines_by_blanks(trimmed); let mut unioned_sum = 0; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0272485..5f87317 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -36,12 +36,29 @@ pub fn read_lines_file(filename: &str) -> Result> { Ok(input) } -pub fn group_lines_by_blanks(lines: Vec) -> Result>> { +pub fn trim_lines<'a>(lines: &'a Vec) -> Vec<&str> { + let mut trimmed_lines = Vec::new(); + + for line in lines { + match line.strip_suffix("\n") { + Some(stripped) => { + trimmed_lines.push(stripped); + } + None => { + trimmed_lines.push(line); + } + } + } + + trimmed_lines +} + +pub fn group_lines_by_blanks(lines: Vec<&str>) -> Vec> { let mut all_groups = Vec::new(); let mut current_group = Vec::new(); for line in lines { - if line.trim().len() == 0 { + if line.len() == 0 { all_groups.push(current_group); current_group = Vec::new(); } else { @@ -53,7 +70,7 @@ pub fn group_lines_by_blanks(lines: Vec) -> Result>> { all_groups.push(current_group); } - Ok(all_groups) + all_groups } pub fn read_int_file(filename: &str) -> Result> { -- cgit 1.4.1