This commit is contained in:
2016-12-03 17:07:18 +01:00
parent 4042eaf9f0
commit d419ccf025
5 changed files with 1162 additions and 0 deletions

4
2015/05/Cargo.lock generated Normal file
View File

@@ -0,0 +1,4 @@
[root]
name = "05"
version = "0.1.0"

6
2015/05/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "05"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

1000
2015/05/input.txt Normal file

File diff suppressed because it is too large Load Diff

151
2015/05/src/main.rs Normal file
View File

@@ -0,0 +1,151 @@
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let handle = stdin.lock();
let input = handle.lines()
.filter_map(|x| x.ok())
.collect::<Vec<_>>();
let count_1 = input.iter()
.filter(|x| vowels(&x) > 2)
.filter(|x| double_letters(&x) > 0)
.filter(|x| !contains(&x, &["ab", "cd", "pq", "xy"]))
.count();
println!("count_1={}", count_1);
let count_2 = input.iter()
.filter(|x| doubles_exists(&x))
.filter(|x| letter_repeats(&x) > 0)
.count();
println!("count_2={}", count_2);
}
fn contains(input: &str, needles: &[&str]) -> bool {
for needle in needles {
if input.contains(needle) {
return true;
}
}
false
}
fn vowels(input: &str) -> usize {
input.chars()
.filter(|&x| "aeiou".contains(x))
.count()
}
fn double_letters(input: &str) -> usize {
input.chars()
.collect::<Vec<char>>()
.windows(2)
.filter(|x| x[0] == x[1])
.count()
}
fn letter_repeats(input: &str) -> usize {
input.chars()
.collect::<Vec<char>>()
.windows(3)
.filter(|x| x[0] == x[2])
.count()
}
fn doubles_exists(input: &str) -> bool {
for n in 0..input.len() - 1 {
if *(&input[n + 2..].contains(&input[n..n + 2])) {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::{contains, vowels, double_letters, letter_repeats, doubles_exists};
#[test]
fn example_01() {
let input = "ugknbfddgicrmopn";
assert_eq!(false, contains(input, &["ab", "cd", "pq", "xy"]));
assert_eq!(3, vowels(input));
assert_eq!(1, double_letters(input));
}
#[test]
fn example_02() {
let input = "aaa";
assert_eq!(false, contains(input, &["ab", "cd", "pq", "xy"]));
assert_eq!(3, vowels(input));
assert_eq!(2, double_letters(input));
}
#[test]
fn example_03() {
let input = "jchzalrnumimnmhp";
assert_eq!(false, contains(input, &["ab", "cd", "pq", "xy"]));
assert_eq!(3, vowels(input));
assert_eq!(0, double_letters(input));
}
#[test]
fn example_04() {
let input = "haegwjzuvuyypxyu";
assert_eq!(true, contains(input, &["ab", "cd", "pq", "xy"]));
assert_eq!(5, vowels(input));
assert_eq!(1, double_letters(input));
}
#[test]
fn example_05() {
let input = "dvszwmarrgswjxmb";
assert_eq!(false, contains(input, &["ab", "cd", "pq", "xy"]));
assert_eq!(1, vowels(input));
assert_eq!(1, double_letters(input));
}
#[test]
fn example_06() {
let input = "qjhvhtzxzqqjkmpb";
assert_eq!(2, letter_repeats(input));
assert_eq!(true, doubles_exists(input));
}
#[test]
fn example_07() {
let input = "xxyxx";
assert_eq!(1, letter_repeats(input));
assert_eq!(true, doubles_exists(input));
}
#[test]
fn example_08() {
let input = "uurcxstgmygtbstg";
assert_eq!(0, letter_repeats(input));
assert_eq!(true, doubles_exists(input));
}
#[test]
fn example_09() {
let input = "ieodomkazucvgmuy";
assert_eq!(1, letter_repeats(input));
assert_eq!(false, doubles_exists(input));
}
}

View File

@@ -1,5 +1,6 @@
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();