diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-11-01 03:05:00 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-11-01 03:05:00 +0000 |
commit | 389b39b1d5288d6c9051425ea4d91d1846072163 (patch) | |
tree | 2e6f057c0811aee65a59c3934cf5d8275c03a38e /2015/puzzle-5.rs | |
parent | 928dcb5572d7c290594625c82b06b4faaa0d712f (diff) | |
download | advent-of-code-389b39b1d5288d6c9051425ea4d91d1846072163.tar.gz advent-of-code-389b39b1d5288d6c9051425ea4d91d1846072163.tar.bz2 advent-of-code-389b39b1d5288d6c9051425ea4d91d1846072163.zip |
Puzzle 5 part 1 of 2015 in rust (eww)
Diffstat (limited to '2015/puzzle-5.rs')
-rw-r--r-- | 2015/puzzle-5.rs | 43 |
1 files changed, 43 insertions, 0 deletions
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: |