parent
e402732959
commit
7afc3ba714
@ -1,42 +1,27 @@
|
||||
use std::io;
|
||||
|
||||
use crypto::md5::Md5;
|
||||
use crypto::digest::Digest;
|
||||
|
||||
use day::Day;
|
||||
|
||||
pub struct Day04 {
|
||||
secret: String,
|
||||
}
|
||||
|
||||
impl Day for Day04 {
|
||||
fn new(input: String) -> Day04 {
|
||||
Day04 { secret: input }
|
||||
}
|
||||
|
||||
fn solve(&self) -> io::Result<i32> {
|
||||
let mut md5 = Md5::new();
|
||||
let key = self.secret.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut out = [0; 16];
|
||||
loop {
|
||||
i += 1;
|
||||
pub fn solve(input: &str) -> i32 {
|
||||
let mut md5 = Md5::new();
|
||||
let key = input.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut out = [0; 16];
|
||||
loop {
|
||||
i += 1;
|
||||
|
||||
md5.input(key);
|
||||
md5.input(i.to_string().as_bytes());
|
||||
md5.result(&mut out);
|
||||
if out[0] == 0 && out[1] == 0 && out[2] == 0 {
|
||||
break;
|
||||
}
|
||||
md5.reset();
|
||||
md5.input(key);
|
||||
md5.input(i.to_string().as_bytes());
|
||||
md5.result(&mut out);
|
||||
if out[0] == 0 && out[1] == 0 && out[2] == 0 {
|
||||
break;
|
||||
}
|
||||
Ok(i)
|
||||
md5.reset();
|
||||
}
|
||||
i
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_day04() {
|
||||
let day = Day04::new("abcdef".to_string());
|
||||
assert_eq!(609043, day.solve().unwrap());
|
||||
assert_eq!(609043, solve("abcdef"));
|
||||
}
|
||||
|
@ -1,59 +1,44 @@
|
||||
use std::io;
|
||||
#![allow(dead_code)]
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use day::Day;
|
||||
|
||||
pub struct Day05 {
|
||||
input: String,
|
||||
pub fn solve(input: &str) -> i32 {
|
||||
input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Day05 {
|
||||
fn is_nice(string: &String) -> bool {
|
||||
Day05::has_pair_of_two_letters(string) &&
|
||||
Day05::has_letter_sandwich(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 {
|
||||
string.as_bytes().windows(2).any(|win| win[0] == win[1])
|
||||
}
|
||||
fn is_nice(string: &String) -> bool {
|
||||
has_pair_of_two_letters(string) &&
|
||||
has_letter_sandwich(string)
|
||||
}
|
||||
|
||||
fn has_no_substrings(string: &String) -> bool {
|
||||
!vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
|
||||
}
|
||||
fn has_three_vowels(string: &String) -> bool {
|
||||
let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
|
||||
three_vowels.is_match(string)
|
||||
}
|
||||
|
||||
fn has_pair_of_two_letters(string: &String) -> bool {
|
||||
string.as_bytes().windows(2).any(|win| {
|
||||
let s = String::from_utf8(win.to_vec()).unwrap();
|
||||
string.split(&s).count() > 2
|
||||
})
|
||||
}
|
||||
fn has_double_letters(string: &String) -> bool {
|
||||
string.as_bytes().windows(2).any(|win| win[0] == win[1])
|
||||
}
|
||||
|
||||
fn has_letter_sandwich(string: &String) -> bool {
|
||||
string.as_bytes().windows(3).any(|win| win[0] == win[2])
|
||||
}
|
||||
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 has_pair_of_two_letters(string: &String) -> bool {
|
||||
string.as_bytes().windows(2).any(|win| {
|
||||
let s = String::from_utf8(win.to_vec()).unwrap();
|
||||
string.split(&s).count() > 2
|
||||
})
|
||||
}
|
||||
|
||||
fn solve(&self) -> io::Result<i32> {
|
||||
Ok(self.input.lines().filter(|&s| Day05::is_nice(&s.to_string())).count() as i32)
|
||||
}
|
||||
fn has_letter_sandwich(string: &String) -> bool {
|
||||
string.as_bytes().windows(3).any(|win| win[0] == win[2])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nice() {
|
||||
assert!( Day05::is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
|
||||
assert!( Day05::is_nice(&"xxyxx ".to_string()));
|
||||
assert!(!Day05::is_nice(&"uurcxstgmygtbstg ".to_string()));
|
||||
assert!(!Day05::is_nice(&"ieodomkazucvgmuy ".to_string()));
|
||||
assert!( is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
|
||||
assert!( is_nice(&"xxyxx ".to_string()));
|
||||
assert!(!is_nice(&"uurcxstgmygtbstg ".to_string()));
|
||||
assert!(!is_nice(&"ieodomkazucvgmuy ".to_string()));
|
||||
}
|
||||
|
Loading…
Reference in new issue