summaryrefslogtreecommitdiff
path: root/2018/src/day2.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/day2.rs')
-rw-r--r--2018/src/day2.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/2018/src/day2.rs b/2018/src/day2.rs
new file mode 100644
index 0000000..6564938
--- /dev/null
+++ b/2018/src/day2.rs
@@ -0,0 +1,64 @@
+use std::io;
+use std::io::BufRead;
+use std::collections::HashMap;
+
+fn close_enough(a: &str, b: &str) -> bool {
+ let mut num = 0;
+
+ for (ch1, ch2) in a.chars().zip(b.chars()) {
+ if ch1 != ch2 {
+ num += 1;
+ if num > 1 { return false; }
+ }
+ }
+
+ return num == 1;
+}
+
+pub fn main<T: BufRead>(reader: T) -> io::Result<()> {
+ let lines: Vec<String> = reader.lines().map(|l| l.unwrap()).collect();
+
+ let mut num2: i64 = 0;
+ let mut num3: i64 = 0;
+
+ for line in &lines {
+ let mut hist = HashMap::new();
+
+ for ch in line.chars() {
+ let value: i64 = *hist.get(&ch).unwrap_or(&0) + 1;
+ hist.insert(ch, value);
+ }
+
+ let mut have2 = false;
+ let mut have3 = false;
+ for &num in hist.values() {
+ if num == 2 { have2 = true; }
+ if num == 3 { have3 = true; }
+ }
+
+ num2 += have2 as i64;
+ num3 += have3 as i64;
+ }
+
+ println!("{}", num2 * num3);
+
+ // TODO: Can this be faster than n^2?
+ for line1 in &lines {
+ for line2 in &lines {
+ if !close_enough(&line1, &line2) { continue; }
+
+ let mut ans = String::new();
+
+ for (ch1, ch2) in line1.chars().zip(line2.chars()) {
+ if ch1 == ch2 {
+ ans.push(ch1);
+ }
+ }
+
+ println!("{}", ans);
+ return Ok(())
+ }
+ }
+
+ Ok(())
+}