diff options
author | June McEnroe <programble@gmail.com> | 2017-11-27 17:11:18 -0500 |
---|---|---|
committer | June McEnroe <programble@gmail.com> | 2017-11-27 17:11:18 -0500 |
commit | b5da2fa328ad63d683d66da65e656ed2ee64878c (patch) | |
tree | 5f8805c3f09817d6d473f91bfd2711643906138b /2016/src/bin/day05.rs | |
parent | License ISC (diff) | |
download | aoc-b5da2fa328ad63d683d66da65e656ed2ee64878c.tar.gz aoc-b5da2fa328ad63d683d66da65e656ed2ee64878c.zip |
Move to 2016 directory
Diffstat (limited to '2016/src/bin/day05.rs')
-rw-r--r-- | 2016/src/bin/day05.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/2016/src/bin/day05.rs b/2016/src/bin/day05.rs new file mode 100644 index 0000000..eefa3df --- /dev/null +++ b/2016/src/bin/day05.rs @@ -0,0 +1,71 @@ +extern crate crypto; + +use std::io::{self, Read}; + +use crypto::digest::Digest; +use crypto::md5::Md5; + +fn solve1(input: &str) -> String { + let mut password = String::new(); + let mut index = 0u64; + + while password.len() < 8 { + let mut md5 = Md5::new(); + md5.input_str(input); + md5.input_str(&index.to_string()); + let digest = md5.result_str(); + + if &digest[0..5] == "00000" { + password.push_str(&digest[5..6]); + } + + index += 1; + } + + password +} + +fn solve2(input: &str) -> String { + let mut password = [None; 8]; + let mut index = 0u64; + + while !password.iter().all(Option::is_some) { + let mut md5 = Md5::new(); + md5.input_str(input); + md5.input_str(&index.to_string()); + let digest = md5.result_str(); + + if &digest[0..5] == "00000" { + if let Some(pos @ 0 ... 7) = digest[5..6].parse().ok() { + password[pos] = password[pos].or(digest[6..7].chars().next()); + } + } + + index += 1; + } + + password.iter() + .cloned() + .map(Option::unwrap) + .collect() +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + println!("Part 1: {}", solve1(input.trim())); + println!("Part 2: {}", solve2(input.trim())); +} + +#[test] +#[ignore] +fn part1() { + assert_eq!("18f47a30", solve1("abc")); +} + +#[test] +#[ignore] +fn part2() { + assert_eq!("05ace8e3", solve2("abc")); +} |