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(reader: T) -> io::Result<(String, String)> { let lines: Vec = 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; } let part1 = 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); } } return Ok((part1.to_string(), ans)); } } Err(io::Error::new(io::ErrorKind::Other, "Invalid input")) }