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
//! # High-Entropy Passphrases
//!
//! ## Part One
//!
//! We use a [`FastSet`] to detect duplicates. Sorting the words in each line
//! then checking for duplicates in adjacent values also works but is slower.
//!
//! ## Part Two
//!
//! To detect anagrams we first convert each word into a histogram of its letter frequency values.
//! As the cardinality is at most 26 we can use a fixed size array to represent the set.
//!
//! Then a [`FastSet`] is used to detect duplicates. Sorting the letters in each word so that
//! anagrams become the same also works but is slower.
use crate::util::hash::*;
type Input<'a> = Vec<&'a str>;
pub fn parse(input: &str) -> Input<'_> {
input.lines().collect()
}
pub fn part1(input: &Input<'_>) -> usize {
let mut seen = FastSet::new();
input
.iter()
.filter(|line| {
seen.clear();
line.split_ascii_whitespace().all(|token| seen.insert(token.as_bytes()))
})
.count()
}
pub fn part2(input: &Input<'_>) -> usize {
// Calculate the frequency of each letter as anagrams will have the same values.
fn convert(token: &str) -> [u8; 26] {
let mut freq = [0; 26];
for b in token.bytes() {
freq[(b - b'a') as usize] += 1;
}
freq
}
let mut seen = FastSet::new();
input
.iter()
.filter(|line| {
seen.clear();
line.split_ascii_whitespace().all(|token| seen.insert(convert(token)))
})
.count()
}