summary refs log tree commit diff
path: root/06/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-12-06 01:01:19 -0800
committerIrene Knapp <ireneista@gmail.com>2020-12-06 01:01:19 -0800
commit08a18531f04a395a556444ecba149356eeddf9d7 (patch)
tree0b46438741947d7971fc62e139c580a8ed61842d /06/src
parent6a3b945f0a4cdf2a27adf915e8a237077fef9f30 (diff)
Grouping primitive in the library
Diffstat (limited to '06/src')
-rw-r--r--06/src/main.rs89
1 files changed, 30 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(())
 }