solution: day5

This commit is contained in:
Matej Janezic 2022-11-30 00:16:13 +01:00
parent 00000050d1
commit 00000060f7
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 80 additions and 0 deletions

1
Cargo.lock generated
View File

@ -9,6 +9,7 @@ dependencies = [
"dotenv",
"hex-literal",
"itertools",
"lazy_static",
"pico-args",
"reqwest",
"rust-crypto",

View File

@ -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"

78
src/bin/05.rs Normal file
View File

@ -0,0 +1,78 @@
use itertools::Itertools;
use lazy_static::lazy_static;
pub fn part_one(input: &str) -> Option<u32> {
let mut count = 0;
lazy_static! {
static ref VOWELS: Vec<char> = 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<u32> {
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));
}
}