diff --git a/Cargo.lock b/Cargo.lock index b2daae0..021e978 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,7 @@ dependencies = [ "dotenv", "hex-literal", "itertools", + "lazy_static", "pico-args", "reqwest", "rust-crypto", diff --git a/Cargo.toml b/Cargo.toml index 84a005b..b5a7d7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ default-run = "aoc" dotenv = "0.15.0" hex-literal = "0.3.4" itertools = "0.10.5" +lazy_static = "1.4.0" pico-args = "0.5.0" reqwest = { version = "0.11.13", features = ["blocking"] } rust-crypto = "0.2.36" diff --git a/src/bin/05.rs b/src/bin/05.rs new file mode 100644 index 000000000..58092bd --- /dev/null +++ b/src/bin/05.rs @@ -0,0 +1,78 @@ +use itertools::Itertools; +use lazy_static::lazy_static; +pub fn part_one(input: &str) -> Option { + let mut count = 0; + + lazy_static! { + static ref VOWELS: Vec = vec!['a', 'e', 'i', 'o', 'u']; + }; + + lazy_static! { + static ref PAIRS: Vec<(char, char)> = vec![('a', 'b'), ('c', 'd'), ('p', 'q'), ('x', 'y')]; + }; + + for keyword in input.to_ascii_lowercase().split('\n') { + let vowels = keyword.chars().filter(|c| VOWELS.contains(c)).count(); + if vowels < 3 { + continue; + }; + let mut double = false; + let mut contains_forbidden = false; + for (letter, next) in keyword.chars().tuple_windows() { + contains_forbidden |= PAIRS.contains(&(letter, next)); + if letter == next { + double = true; + } + } + if double && !contains_forbidden { + count += 1; + }; + } + Some(count) +} + +pub fn part_two(input: &str) -> Option { + let mut count = 0; + + for keyword in input.to_ascii_lowercase().split('\n') { + let mut split_repeat = false; + for (a, _, c) in keyword.chars().tuple_windows() { + split_repeat |= a == c; + } + + if !split_repeat { + continue; + }; + + for i in 2..keyword.len() { + if keyword[i..].contains(&keyword[i - 2..i]) { + count += 1; + break; + } + } + } + + Some(count) +} +fn main() { + let input = &aoc::read_file("inputs", 5); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + // let input = aoc::read_file("test_inputs", 5); + let input = "aaa"; + assert_eq!(part_one(&input), Some(1)); + } + #[test] + fn test_part_two() { + // let input = aoc::read_file("test_inputs", 5); + let input = + "qjhvhtzxzqqjkmpb\nxxyxx\nuurcxstgmygtbstg\nieodomkazucvgmuy\naaa"; + assert_eq!(part_two(&input), Some(2)); + } +}