58 lines
1.1 KiB
Rust
58 lines
1.1 KiB
Rust
use std::io::Read;
|
|
use std::collections::HashSet;
|
|
|
|
|
|
pub fn part1<F: Read> (mut input: F) {
|
|
let mut buf = String::new();
|
|
input.read_to_string(&mut buf).unwrap();
|
|
let sum: usize = buf.split("\n\n")
|
|
.map(|group|
|
|
group.replace('\n', "")
|
|
.chars()
|
|
.collect::<HashSet<_>>()
|
|
.len()
|
|
).sum();
|
|
println!("{}", sum);
|
|
}
|
|
|
|
fn compute_part2<F: Read> (mut input: F) -> usize {
|
|
let mut buf = String::new();
|
|
input.read_to_string(&mut buf).unwrap();
|
|
buf.split("\n\n")
|
|
.map(|group|
|
|
group.lines()
|
|
.map(|person| person.chars().collect::<HashSet<_>>())
|
|
.fold_first(|acc, p| acc.intersection(&p).map(char::to_owned).collect())
|
|
.unwrap().len()
|
|
).sum()
|
|
}
|
|
|
|
pub fn part2<F: Read> (input: F) {
|
|
println!("{}", compute_part2(input));
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
#[test]
|
|
pub fn test_parse() {
|
|
let input = r#"abc
|
|
|
|
a
|
|
b
|
|
c
|
|
|
|
ab
|
|
ac
|
|
|
|
a
|
|
a
|
|
a
|
|
a
|
|
|
|
b"#.as_bytes();
|
|
assert_eq!(6, compute_part2(input))
|
|
}
|
|
}
|