summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--06/Cargo.toml8
-rw-r--r--06/src/main.rs93
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
4 files changed, 109 insertions, 0 deletions
diff --git a/06/Cargo.toml b/06/Cargo.toml
new file mode 100644
index 0000000..e415fff
--- /dev/null
+++ b/06/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "advent_06"
+version = "0.1.0"
+authors = ["Irene Knapp <ireneista@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+advent_lib = { path = "../lib" }
diff --git a/06/src/main.rs b/06/src/main.rs
new file mode 100644
index 0000000..4cc94f6
--- /dev/null
+++ b/06/src/main.rs
@@ -0,0 +1,93 @@
+use advent_lib::prelude::*;
+
+use std::collections::BTreeSet;
+
+
+fn main() -> Result<()> {
+  let mut args = std::env::args();
+  if args.len() != 2 {
+    eprintln!("Usage: advent input");
+  }
+  let _ = args.next();
+  let filename = args.next().unwrap();
+
+  let input = advent_lib::read_lines_file(&filename)?;
+
+  let mut yes_counts = Vec::new();
+  let mut yes_answers = BTreeSet::new();
+
+  for line in &input {
+    if line.trim().len() == 0 {
+      let count = yes_answers.len();
+      yes_counts.push(count);
+
+      yes_answers = BTreeSet::new();
+    }
+
+    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;
+  }
+
+  println!("{}", sum);
+
+  let mut everyone_yes_counts = Vec::new();
+  let mut everyone_yes_answers = BTreeSet::new();
+  let mut is_within_group = false;
+
+  for line in &input {
+    let this_line_is_blank = line.trim().len() == 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;
+    }
+
+    let mut this_person_yes_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());
+      }
+
+      everyone_yes_answers = copied;
+    } else {
+      everyone_yes_answers = this_person_yes_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;
+  }
+
+  println!("{}", everyone_sum);
+
+  Ok(())
+}
diff --git a/Cargo.lock b/Cargo.lock
index 8ccce11..0be294d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -38,6 +38,13 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_06"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 
diff --git a/Cargo.toml b/Cargo.toml
index 682e0be..6a830cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,4 +6,5 @@ members = [
   "03",
   "04",
   "05",
+  "06",
 ]