2018/02
This commit is contained in:
60
2018/02/src/main.rs
Normal file
60
2018/02/src/main.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn diff(left: &str, right: &str) -> Vec<(usize, (char, char))> {
|
||||
left.chars()
|
||||
.zip(right.chars())
|
||||
.enumerate()
|
||||
.filter(|(_, (a, b))| a != b)
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let stdin = io::stdin();
|
||||
|
||||
let lines = stdin
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.map(|line| {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for ch in line.chars() {
|
||||
map.entry(ch).and_modify(|x| *x += 1).or_insert(1);
|
||||
}
|
||||
|
||||
(line, map)
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
let two = lines
|
||||
.iter()
|
||||
.filter(|(_, map)| map.iter().any(|(_, count)| *count == 2))
|
||||
.count();
|
||||
let three = lines
|
||||
.iter()
|
||||
.filter(|(_, map)| map.iter().any(|(_, count)| *count == 3))
|
||||
.count();
|
||||
|
||||
println!("part.one={}", two * three);
|
||||
|
||||
for (i, (left, _)) in lines.iter().enumerate() {
|
||||
let diff = lines
|
||||
.iter()
|
||||
.skip(i + 1)
|
||||
.map(|(right, _)| diff(left, right))
|
||||
.find(|diff| diff.len() == 1)
|
||||
.map(|mut diff| diff.pop().unwrap());
|
||||
|
||||
if let Some((n, _)) = diff {
|
||||
let string = left
|
||||
.chars()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| *i != n)
|
||||
.map(|(_, c)| c)
|
||||
.collect::<String>();
|
||||
|
||||
println!("part.two={:?}", string);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user