[rust] Day 4.0

wip
Alpha Chen 9 years ago
parent 4a90bee104
commit e3fcf73113

87
rust/Cargo.lock generated

@ -1,4 +1,91 @@
[root]
name = "advent_of_code"
version = "0.1.0"
dependencies = [
"rust-crypto 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (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 = "gcc"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (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 = "libc"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rust-crypto"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "time"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

@ -2,3 +2,6 @@
name = "advent_of_code"
version = "0.1.0"
authors = ["Alpha Chen <alpha.chen@gmail.com>"]
[dependencies]
rust-crypto = "^0.2"

@ -0,0 +1,42 @@
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;
md5.input(key);
md5.input(i.to_string().as_bytes());
md5.result(&mut out);
if out[0] == 0 && out[1] == 0 && out[2] >> 4 == 0 {
break;
}
md5.reset();
}
Ok(i)
}
}
#[test]
#[ignore]
fn test_day04() {
let day = Day04::new("abcdef".to_string());
assert_eq!(609043, day.solve().unwrap());
}

@ -1,3 +1,5 @@
extern crate crypto;
macro_rules! import_day {
( $( $d:ident ),* ) => {
$(
@ -10,4 +12,5 @@ macro_rules! import_day {
import_day!(day,
day_01,
day_02,
day_03);
day_03,
day_04);

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

Loading…
Cancel
Save