diff --git a/Cargo.lock b/Cargo.lock index 58e58a1..38d0058 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,7 @@ name = "aoc2015" version = "0.1.0" dependencies = [ "aoc 0.1.0", + "md-5 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -25,3 +26,78 @@ dependencies = [ "aoc 0.1.0", ] +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "generic-array" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "md-5" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "typenum" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +"checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" +"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +"checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +"checksum md-5 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a18af3dcaf2b0219366cdb4e2af65a6101457b415c3d1a5c71dd9c2b7c77b9c8" +"checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" diff --git a/aoc2015/Cargo.toml b/aoc2015/Cargo.toml index 6749987..279ebf8 100644 --- a/aoc2015/Cargo.toml +++ b/aoc2015/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" [dependencies] aoc = { path = "../aoc" } +md-5 = "0.8" diff --git a/aoc2015/input/day04.txt b/aoc2015/input/day04.txt new file mode 100644 index 0000000..d75e0dc --- /dev/null +++ b/aoc2015/input/day04.txt @@ -0,0 +1 @@ +yzbqklnj diff --git a/aoc2015/src/day04.rs b/aoc2015/src/day04.rs new file mode 100644 index 0000000..222129f --- /dev/null +++ b/aoc2015/src/day04.rs @@ -0,0 +1,48 @@ +use md5::{Digest, Md5}; + +use aoc::err; +use aoc::Result; + +const INPUT: &str = include_str!("../input/day04.txt"); + +pub fn run() -> Result<()> { + println!("part 1: {}", part1(INPUT)?); + + Ok(()) +} + +fn part1(input: &str) -> Result { + let input = input.trim_end(); + + let mut hasher = Md5::new(); + + for i in 0.. { + let content = format!("{}{}", input, i); + + hasher.input(content); + let res = format!("{:x}", hasher.result_reset()); + if &res[..5] == "00000" { + return Ok(i); + } + } + + Err(err!("couldn't find a suitable number")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore] // takes too long! + fn part1_provided() { + assert_eq!(part1("abcdef").unwrap(), 609043); + assert_eq!(part1("pqrstuv").unwrap(), 1048970); + } + + #[test] + #[ignore] // takes too long! + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 282749); + } +} diff --git a/aoc2015/src/lib.rs b/aoc2015/src/lib.rs index 89345b1..e051fbd 100644 --- a/aoc2015/src/lib.rs +++ b/aoc2015/src/lib.rs @@ -1,3 +1,4 @@ pub mod day01; pub mod day02; pub mod day03; +pub mod day04; diff --git a/aoc2015/src/main.rs b/aoc2015/src/main.rs index 68fad4b..fee1657 100644 --- a/aoc2015/src/main.rs +++ b/aoc2015/src/main.rs @@ -3,9 +3,10 @@ use aoc::Result; use aoc2015::day01; use aoc2015::day02; use aoc2015::day03; +use aoc2015::day04; fn main() -> Result<()> { - let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run]; + let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run]; aoc::run(days) }