Finish part 2 of puzzle 5 for 2015.
This commit is contained in:
@@ -1,27 +1,29 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
const VOWELS_STR: &str = "aeiou";
|
||||||
|
const DO_NOT_WANT: [&str; 4] = ["ab", "cd", "pq", "xy"];
|
||||||
|
|
||||||
fn is_nice_round1(line: &str) -> bool {
|
fn is_nice_round1(line: &str) -> bool {
|
||||||
let do_not_want: [&str; 4] = ["ab", "cd", "pq", "xy"];
|
|
||||||
let mut unwanted_substr = false;
|
let mut unwanted_substr = false;
|
||||||
let mut has_consecutive = false;
|
let mut has_consecutive = false;
|
||||||
let mut vowels = 0;
|
let mut vowels = 0;
|
||||||
for i in 0..line.len() - 1 {
|
for i in 0..line.len() - 1 {
|
||||||
let substr: &str = &line[i..i + 2];
|
let slide: &str = &line[i..i + 2];
|
||||||
if do_not_want.iter().position(|&x| x == substr).is_some() {
|
if DO_NOT_WANT.iter().position(|&x| x == slide).is_some() {
|
||||||
unwanted_substr = true;
|
unwanted_substr = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
let first = slide.chars().nth(0).unwrap();
|
||||||
if "aeiou".contains(substr.chars().nth(0).unwrap()) {
|
let second = slide.chars().nth(1).unwrap();
|
||||||
|
if VOWELS_STR.contains(first) {
|
||||||
vowels += 1;
|
vowels += 1;
|
||||||
}
|
}
|
||||||
|
if first == second {
|
||||||
if substr.chars().nth(0).unwrap() == substr.chars().nth(1).unwrap() {
|
|
||||||
has_consecutive = true;
|
has_consecutive = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if "aeiou".contains(line.chars().nth(line.len() - 1).unwrap()) {
|
if VOWELS_STR.contains(line.chars().nth(line.len() - 1).unwrap()) {
|
||||||
vowels += 1;
|
vowels += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,11 +33,46 @@ fn is_nice_round1(line: &str) -> bool {
|
|||||||
return vowels >= 3 && has_consecutive;
|
return vowels >= 3 && has_consecutive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_nice_round2(line: &str) -> bool {
|
||||||
|
/*
|
||||||
|
Justifying the O(|line|^2) runtime is easy: |line| is constant, such that at
|
||||||
|
worst we're doing roughly O(256) iterations. Not too bad.
|
||||||
|
|
||||||
|
If |line| could be really big I'd look into optimising the rest, but why do
|
||||||
|
that?
|
||||||
|
*/
|
||||||
|
let mut has_pair = false;
|
||||||
|
|
||||||
|
// Unfortunate O(|line|^2) runtime
|
||||||
|
for i in 0..line.len() - 1 {
|
||||||
|
let slide = &line[i..i + 2];
|
||||||
|
let rest = &line[i + 2..];
|
||||||
|
if rest.contains(slide) {
|
||||||
|
has_pair = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// O(|line|) runtime
|
||||||
|
let mut has_triple = false;
|
||||||
|
for i in 0..line.len() - 2 {
|
||||||
|
let slide = &line[i..i + 3];
|
||||||
|
if slide.chars().nth(0) == slide.chars().nth(2) {
|
||||||
|
has_triple = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return has_pair && has_triple;
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let binding = fs::read_to_string("5-input").expect("wget 5-input please.");
|
let binding = fs::read_to_string("5-input").expect("wget 5-input please.");
|
||||||
let contents: Vec<&str> = binding.split("\n").collect();
|
let contents: Vec<&str> = binding.split("\n").collect();
|
||||||
let nice_lines_1 = contents.iter().filter(|&x| is_nice_round1(x)).count();
|
let nice_lines_1 = contents.iter().filter(|&x| is_nice_round1(x)).count();
|
||||||
|
let nice_lines_2 = contents.iter().filter(|&x| is_nice_round2(x)).count();
|
||||||
println!("Round 1: {nice_lines_1}");
|
println!("Round 1: {nice_lines_1}");
|
||||||
|
println!("Round 2: {nice_lines_2}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
|
|||||||
Reference in New Issue
Block a user