[rust] Day 5.0

wip
Alpha Chen 9 years ago
parent 180a73c1d1
commit 3e2cbb4454

32
rust/Cargo.lock generated

@ -2,6 +2,7 @@
name = "advent_of_code" name = "advent_of_code"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"regex 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -14,6 +15,14 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "aho-corasick"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "gcc" name = "gcc"
version = "0.3.21" version = "0.3.21"
@ -42,6 +51,14 @@ name = "libc"
version = "0.2.4" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.3.12" version = "0.3.12"
@ -52,6 +69,21 @@ dependencies = [
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "regex"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "rust-crypto" name = "rust-crypto"
version = "0.2.34" version = "0.2.34"

@ -4,4 +4,5 @@ version = "0.1.0"
authors = ["Alpha Chen <alpha.chen@gmail.com>"] authors = ["Alpha Chen <alpha.chen@gmail.com>"]
[dependencies] [dependencies]
regex = "0.1"
rust-crypto = "^0.2" rust-crypto = "^0.2"

File diff suppressed because it is too large Load Diff

@ -0,0 +1,54 @@
use std::io;
use regex::Regex;
use day::Day;
pub struct Day05 {
input: String,
}
impl Day05 {
fn is_nice(string: &String) -> bool {
Day05::has_three_vowels(string) &&
Day05::has_double_letters(string) &&
Day05::has_no_substrings(string)
}
fn has_three_vowels(string: &String) -> bool {
let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
three_vowels.is_match(string)
}
fn has_double_letters(string: &String) -> bool {
for win in string.as_bytes().windows(2) {
if win[0] == win[1] {
return true;
}
}
false
}
fn has_no_substrings(string: &String) -> bool {
!vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
}
}
impl Day for Day05 {
fn new(input: String) -> Day05 {
Day05 { input: input }
}
fn solve(&self) -> io::Result<i32> {
Ok(self.input.lines().filter(|&s| Day05::is_nice(&s.to_string())).count() as i32)
}
}
#[test]
fn test_nice() {
assert!( Day05::is_nice(&"ugknbfddgicrmopn".to_string()));
assert!( Day05::is_nice(&"aaa".to_string()));
assert!(!Day05::is_nice(&"jchzalrnumimnmhp".to_string()));
assert!(!Day05::is_nice(&"haegwjzuvuyypxyu".to_string()));
assert!(!Day05::is_nice(&"dvszwmarrgswjxmb".to_string()));
}

@ -1,4 +1,5 @@
extern crate crypto; extern crate crypto;
extern crate regex;
macro_rules! import_day { macro_rules! import_day {
( $( $d:ident ),* ) => { ( $( $d:ident ),* ) => {
@ -9,8 +10,11 @@ macro_rules! import_day {
} }
} }
import_day!(day, import_day! {
day_01, day,
day_02, day_01,
day_03, day_02,
day_04); day_03,
day_04,
day_05
}

@ -19,7 +19,7 @@ fn read_input(filename: &str) -> Result<String, io::Error> {
} }
fn main() { fn main() {
let input = read_input("day_04").unwrap(); let input = read_input("day_05").unwrap();
let day = Day04::new(input); let day = Day05::new(input);
println!("{}", day.solve().unwrap()); println!("{}", day.solve().unwrap());
} }

Loading…
Cancel
Save