From 068f631411ae3e26641c1ac1e7ab208497b935e1 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 17 Dec 2016 00:28:01 -0500 Subject: Day 14 part 2 --- src/bin/day14.rs | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'src/bin') diff --git a/src/bin/day14.rs b/src/bin/day14.rs index b22e88d..352982d 100644 --- a/src/bin/day14.rs +++ b/src/bin/day14.rs @@ -7,17 +7,37 @@ use std::iter; use crypto::digest::Digest; use crypto::md5::Md5; -fn md5(salt: &str, index: u32) -> String { - let mut md5 = Md5::new(); - md5.input_str(salt); - md5.input_str(&index.to_string()); - md5.result_str() +trait Hash { + fn hash(salt: &str, index: u32) -> String; } -fn solve(salt: &str) -> u32 { +struct Part1; +impl Hash for Part1 { + fn hash(salt: &str, index: u32) -> String { + let mut md5 = Md5::new(); + md5.input_str(salt); + md5.input_str(&index.to_string()); + md5.result_str() + } +} + +struct Part2; +impl Hash for Part2 { + fn hash(salt: &str, index: u32) -> String { + let mut hash = Part1::hash(salt, index); + for _ in 0..2016 { + let mut md5 = Md5::new(); + md5.input_str(&hash); + hash = md5.result_str(); + } + hash + } +} + +fn solve(salt: &str) -> u32 { let mut hashes = VecDeque::new(); for i in 0..1001 { - hashes.push_back(md5(salt, i)); + hashes.push_back(H::hash(salt, i)); } let mut keys = 0; @@ -40,7 +60,7 @@ fn solve(salt: &str) -> u32 { } index += 1; - hashes.push_back(md5(salt, 1000 + index)); + hashes.push_back(H::hash(salt, 1000 + index)); } unreachable!() @@ -50,11 +70,18 @@ fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); - println!("Part 1: {}", solve(input.trim())); + println!("Part 1: {}", solve::(input.trim())); + println!("Part 2: {}", solve::(input.trim())); } #[test] #[ignore] fn part1() { - assert_eq!(22728, solve("abc")); + assert_eq!(22728, solve::("abc")); +} + +#[test] +#[ignore] +fn part2() { + assert_eq!(22551, solve::("abc")); } -- cgit 1.4.1 class='list nowrap'>Commit message (Collapse)Author 2018-04-03Ignore c and txtJune McEnroe 2018-04-03Move home bins to ~/.local/binJune McEnroe Also replaced ~/.cargo/bin with a symlink to ~/.local/bin to avoid having to have that in $path as well. 2018-04-02Highlight Special as NormalJune McEnroe Special characters in strings are more like normal code than keywords. 2018-04-02Use size_t for iterating in schemeJune McEnroe 2018-04-02Modulo H and saturate S, V in schemeJune McEnroe 2018-04-02Rewrite scheme in a more sensible orderJune McEnroe 2018-04-02Use function pointers in schemeJune McEnroe 2018-04-02Use union for scheme gen functionsJune McEnroe 2018-04-02Use uint32_t for len in schemeJune McEnroe There are potentially 256 colours. 2018-04-01Add HSV output to schemeJune McEnroe 2018-03-31Output Linux console escapes from schemeJune McEnroe 2018-03-31Add scheme to READMEJune McEnroe 2018-03-31Brighten color schemeJune McEnroe 2018-03-31Set Dark terminal to schemeJune McEnroe 2018-03-31Lighten cursor colorJune McEnroe 2018-03-31Tweak cyan furtherJune McEnroe It's actually green at this point. 2018-03-31Tweak colors in schemeJune McEnroe 2018-03-31Add scheme.png targetJune McEnroe 2018-03-31Generate Terminal.app color schemeJune McEnroe Colors still very much WIP, but coming along. 2018-03-31Generate basic ANSI color schemeJune McEnroe 2018-03-31Add hex output to schemeJune McEnroe 2018-03-31Add color scheme PNG generatorJune McEnroe 2018-03-31Simplify gfxx palette generationJune McEnroe 2018-03-31Switch to HSV for gfxx palette generationJune McEnroe 2018-03-30Generate default palette in gfxxJune McEnroe 2018-03-30Ignore build and cloneJune McEnroe 2018-03-30Set g:clipboard to pb{copy,paste} alwaysJune McEnroe Previously neovim would use these automatically if it found them, but now it only checks for them on macOS. pbd continues to work well. 2018-03-28Add d-_-b crateJune McEnroe