From e3fcf73113be33a6010ee73f9487565a1401d978 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 3 Jan 2016 15:42:06 -0800 Subject: [PATCH] [rust] Day 4.0 --- rust/Cargo.lock | 87 +++++++++++++++++++++++++++++++++++++++++++ rust/Cargo.toml | 3 ++ rust/input/day_04.txt | 1 + rust/src/day_04.rs | 42 +++++++++++++++++++++ rust/src/lib.rs | 5 ++- rust/src/main.rs | 4 +- 6 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 rust/input/day_04.txt create mode 100644 rust/src/day_04.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index c1eb1d7..46ab38e 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -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" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 83e72ab..ece5289 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -2,3 +2,6 @@ name = "advent_of_code" version = "0.1.0" authors = ["Alpha Chen "] + +[dependencies] +rust-crypto = "^0.2" diff --git a/rust/input/day_04.txt b/rust/input/day_04.txt new file mode 100644 index 0000000..73269d7 --- /dev/null +++ b/rust/input/day_04.txt @@ -0,0 +1 @@ +ckczppom \ No newline at end of file diff --git a/rust/src/day_04.rs b/rust/src/day_04.rs new file mode 100644 index 0000000..9e7f2e6 --- /dev/null +++ b/rust/src/day_04.rs @@ -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 { + 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()); +} diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 4c15b7f..6181391 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -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); diff --git a/rust/src/main.rs b/rust/src/main.rs index 3d7bd5e..8408257 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -19,7 +19,7 @@ fn read_input(filename: &str) -> Result { } 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()); }