From 389b39b1d5288d6c9051425ea4d91d1846072163 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 1 Nov 2024 03:05:00 +0000 Subject: Puzzle 5 part 1 of 2015 in rust (eww) --- 2015/puzzle-5.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2015/puzzle-5.rs diff --git a/2015/puzzle-5.rs b/2015/puzzle-5.rs new file mode 100644 index 0000000..c40e337 --- /dev/null +++ b/2015/puzzle-5.rs @@ -0,0 +1,43 @@ +use std::fs; + +fn is_nice_round1(line: &str) -> bool { + let do_not_want: [&str; 4] = ["ab", "cd", "pq", "xy"]; + let mut unwanted_substr = false; + let mut has_consecutive = false; + let mut vowels = 0; + for i in 0..line.len() - 1 { + let substr: &str = &line[i..i + 2]; + if do_not_want.iter().position(|&x| x == substr).is_some() { + unwanted_substr = true; + break; + } + + if "aeiou".contains(substr.chars().nth(0).unwrap()) { + vowels += 1; + } + + if substr.chars().nth(0).unwrap() == substr.chars().nth(1).unwrap() { + has_consecutive = true; + } + } + + if "aeiou".contains(line.chars().nth(line.len() - 1).unwrap()) { + vowels += 1; + } + + if unwanted_substr { + return false; + } + return vowels >= 3 && has_consecutive; +} + +fn main() { + let binding = fs::read_to_string("5-input").expect("wget 5-input please."); + let contents: Vec<&str> = binding.split("\n").collect(); + let nice_lines_1 = contents.iter().filter(|&x| is_nice_round1(x)).count(); + println!("Round 1: {nice_lines_1}"); +} + +// Local Variables: +// compile-command: "rustc puzzle-5.rs && ./puzzle-5" +// End: -- cgit v1.2.3-13-gbd6f