Second huge commit

This commit is contained in:
Russell Champoux committed 2016-12-05 17:37:26 -05:00
1 parent ef848440e5
commit e91f81a898
10 files changed
+387

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
[root]
name = "day05"
version = "0.1.0"
dependencies = [
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (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.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rust-crypto"
version = "0.2.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "time"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.8"
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"
[metadata]
"checksum gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)" = "771e4a97ff6f237cf0f7d5f5102f6e28bb9743814b6198d684da5c58b76c11e0"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "bff9fc1c79f2dec76b253273d07682e94a978bd8f132ded071188122b2af9818"
"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "day05"
version = "0.1.0"
authors = ["~krowbar"]
[dependencies]
rust-crypto = "^0.2"
+79
View File
@@ -0,0 +1,79 @@
extern crate crypto;
use crypto::md5::Md5;
use crypto::digest::Digest;
//This was mostly stolen from a github gist that I found while looking for a Rust MD5 library
fn part_1() {
let mut hasher = Md5::new();
let key = "reyedfim".as_bytes();
let mut answer: Vec<u8> = vec![];
let mut count = 8;
for i in 0..std::u64::MAX {
hasher.input(key);
hasher.input(i.to_string().as_bytes());
let mut output = [0; 16]; //represents MD5 string
hasher.result(&mut output);
//check the first two and a half bytes of the output
let first_five = output[0] as i32 + output[1] as i32 + (output[2] >> 4) as i32;
if first_five == 0 {
let output_str: String = output.iter().map(|&c| format!("{:x}{:x}", c >> 4, c)).collect();
println!("hashed {} to get {}", i, output_str);
answer.push(output[2]);
match count > 1 {
true => count -= 1,
false => break,
}
}
hasher.reset();
}
let answer_str: String = answer.iter().map(|&c| format!("{:x}", c)).collect();
println!("Part 1: Answer is {}", answer_str);
}
fn part_2() {
let mut hasher = Md5::new();
let key = "reyedfim".as_bytes();
let mut answer = [0; 8];
let mut answer_mask = [true; 8];
for i in 0..std::u64::MAX {
hasher.input(key);
hasher.input(i.to_string().as_bytes());
let mut output = [0; 16]; //represents MD5 string
hasher.result(&mut output);
//check the first two and a half bytes of the output
let first_five = output[0] as i32 + output[1] as i32 + (output[2] >> 4) as i32;
if first_five == 0 {
let output_str: String = output.iter().map(|&c| format!("{:x}{:x}", c >> 4, c)).collect();
println!("hashed {} to get {}", i, output_str);
//answer.push(output[2]);
let idx = output[2] as usize;
let val = output[3] >> 4;
if idx < 8 && answer_mask[idx] {
answer[idx] = val;
answer_mask[idx] = false; //we filled this slot
println!("Values are {} and {}", idx, val);
match answer_mask.iter().fold(false, |f, &i| f || i) {
true => {}, //keep going
false => break, //we're all done here
}
}
}
hasher.reset();
}
let answer_str: String = answer.iter().map(|&c| format!("{:x}", c)).collect();
println!("Part 1: Answer is {}", answer_str);
}
fn main() {
//part_1();
part_2();
}