blob: 38fd4bae304e7725808bae6762e0d96b0cd52e32 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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<(String, String)> {
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;
}
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"))
}
|