diff options
-rw-r--r-- | 06/src/main.rs | 89 | ||||
-rw-r--r-- | lib/src/lib.rs | 19 |
2 files changed, 49 insertions, 59 deletions
diff --git a/06/src/main.rs b/06/src/main.rs index 4cc94f6..72e4f1d 100644 --- a/06/src/main.rs +++ b/06/src/main.rs @@ -12,82 +12,53 @@ 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 mut yes_counts = Vec::new(); - let mut yes_answers = BTreeSet::new(); + let mut unioned_sum = 0; - for line in &input { - if line.trim().len() == 0 { - let count = yes_answers.len(); - yes_counts.push(count); + for group in &groups { + let mut yes_answers = BTreeSet::new(); - yes_answers = BTreeSet::new(); - } - - for c in line.trim().chars() { - yes_answers.insert(c); + for line in group { + for c in line.trim().chars() { + yes_answers.insert(c); + } } - } - let count = yes_answers.len(); - yes_counts.push(count); - - let mut sum = 0; - for count in yes_counts { - sum += count; + unioned_sum += yes_answers.len(); } - println!("{}", sum); - - let mut everyone_yes_counts = Vec::new(); - let mut everyone_yes_answers = BTreeSet::new(); - let mut is_within_group = false; + println!("{}", unioned_sum); - for line in &input { - let this_line_is_blank = line.trim().len() == 0; + let mut intersected_sum = 0; - if this_line_is_blank { - let count = everyone_yes_answers.len(); - everyone_yes_counts.push(count); - //println!("{}", count); - - everyone_yes_answers = BTreeSet::new(); - is_within_group = false; - } + for group in &groups { + let mut group_answers = BTreeSet::new(); + let mut is_first_person = true; - let mut this_person_yes_answers = BTreeSet::new(); + for line in group { + let mut person_answers = BTreeSet::new(); - for c in line.trim().chars() { - this_person_yes_answers.insert(c); - } - - //println!("{} {}", everyone_yes_answers.len(), this_person_yes_answers.len()); - if is_within_group { - let intersection = - everyone_yes_answers.intersection(&this_person_yes_answers); - - let mut copied = BTreeSet::new(); - for c in intersection { - copied.insert(c.clone()); + for c in line.trim().chars() { + person_answers.insert(c); } - everyone_yes_answers = copied; - } else { - everyone_yes_answers = this_person_yes_answers; + if is_first_person { + group_answers = person_answers; + is_first_person = false; + } else { + let mut intersected_answers = BTreeSet::new(); + for c in group_answers.intersection(&person_answers) { + intersected_answers.insert(c.clone()); + } + group_answers = intersected_answers; + } } - is_within_group = !this_line_is_blank; - } - - let count = everyone_yes_answers.len(); - everyone_yes_counts.push(count); - - let mut everyone_sum = 0; - for count in everyone_yes_counts { - everyone_sum += count; + intersected_sum += group_answers.len(); } - println!("{}", everyone_sum); + println!("{}", intersected_sum); Ok(()) } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4913cbb..0272485 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -36,6 +36,25 @@ pub fn read_lines_file(filename: &str) -> Result<Vec<String>> { Ok(input) } +pub fn group_lines_by_blanks(lines: Vec<String>) -> Result<Vec<Vec<String>>> { + let mut all_groups = Vec::new(); + let mut current_group = Vec::new(); + + for line in lines { + if line.trim().len() == 0 { + all_groups.push(current_group); + current_group = Vec::new(); + } else { + current_group.push(line); + } + } + + if current_group.len() > 0 { + all_groups.push(current_group); + } + + Ok(all_groups) +} pub fn read_int_file(filename: &str) -> Result<Vec<i64>> { let file = File::open(filename)?; |