solution: day4
This commit is contained in:
		
							
								
								
									
										105
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										105
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -7,9 +7,11 @@ name = "aoc" | |||||||
| version = "0.1.0" | version = "0.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "dotenv", |  "dotenv", | ||||||
|  |  "hex-literal", | ||||||
|  "itertools", |  "itertools", | ||||||
|  "pico-args", |  "pico-args", | ||||||
|  "reqwest", |  "reqwest", | ||||||
|  |  "rust-crypto", | ||||||
| ] | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| @@ -130,6 +132,12 @@ dependencies = [ | |||||||
|  "percent-encoding", |  "percent-encoding", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "fuchsia-cprng" | ||||||
|  | version = "0.1.1" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "futures-channel" | name = "futures-channel" | ||||||
| version = "0.3.25" | version = "0.3.25" | ||||||
| @@ -178,6 +186,12 @@ dependencies = [ | |||||||
|  "slab", |  "slab", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "gcc" | ||||||
|  | version = "0.3.55" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "h2" | name = "h2" | ||||||
| version = "0.3.15" | version = "0.3.15" | ||||||
| @@ -212,6 +226,12 @@ dependencies = [ | |||||||
|  "libc", |  "libc", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "hex-literal" | ||||||
|  | version = "0.3.4" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "http" | name = "http" | ||||||
| version = "0.2.8" | version = "0.2.8" | ||||||
| @@ -383,7 +403,7 @@ checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" | |||||||
| dependencies = [ | dependencies = [ | ||||||
|  "libc", |  "libc", | ||||||
|  "log", |  "log", | ||||||
|  "wasi", |  "wasi 0.11.0+wasi-snapshot-preview1", | ||||||
|  "windows-sys 0.42.0", |  "windows-sys 0.42.0", | ||||||
| ] | ] | ||||||
|  |  | ||||||
| @@ -514,6 +534,53 @@ dependencies = [ | |||||||
|  "proc-macro2", |  "proc-macro2", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rand" | ||||||
|  | version = "0.3.23" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" | ||||||
|  | dependencies = [ | ||||||
|  |  "libc", | ||||||
|  |  "rand 0.4.6", | ||||||
|  | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rand" | ||||||
|  | version = "0.4.6" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" | ||||||
|  | dependencies = [ | ||||||
|  |  "fuchsia-cprng", | ||||||
|  |  "libc", | ||||||
|  |  "rand_core 0.3.1", | ||||||
|  |  "rdrand", | ||||||
|  |  "winapi", | ||||||
|  | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rand_core" | ||||||
|  | version = "0.3.1" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" | ||||||
|  | dependencies = [ | ||||||
|  |  "rand_core 0.4.2", | ||||||
|  | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rand_core" | ||||||
|  | version = "0.4.2" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rdrand" | ||||||
|  | version = "0.4.0" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" | ||||||
|  | dependencies = [ | ||||||
|  |  "rand_core 0.3.1", | ||||||
|  | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "redox_syscall" | name = "redox_syscall" | ||||||
| version = "0.2.16" | version = "0.2.16" | ||||||
| @@ -569,6 +636,25 @@ dependencies = [ | |||||||
|  "winreg", |  "winreg", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rust-crypto" | ||||||
|  | version = "0.2.36" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" | ||||||
|  | dependencies = [ | ||||||
|  |  "gcc", | ||||||
|  |  "libc", | ||||||
|  |  "rand 0.3.23", | ||||||
|  |  "rustc-serialize", | ||||||
|  |  "time", | ||||||
|  | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "rustc-serialize" | ||||||
|  | version = "0.3.24" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "ryu" | name = "ryu" | ||||||
| version = "1.0.11" | version = "1.0.11" | ||||||
| @@ -681,6 +767,17 @@ dependencies = [ | |||||||
|  "winapi", |  "winapi", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "time" | ||||||
|  | version = "0.1.45" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" | ||||||
|  | dependencies = [ | ||||||
|  |  "libc", | ||||||
|  |  "wasi 0.10.0+wasi-snapshot-preview1", | ||||||
|  |  "winapi", | ||||||
|  | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "tinyvec" | name = "tinyvec" | ||||||
| version = "1.6.0" | version = "1.6.0" | ||||||
| @@ -817,6 +914,12 @@ dependencies = [ | |||||||
|  "try-lock", |  "try-lock", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "wasi" | ||||||
|  | version = "0.10.0+wasi-snapshot-preview1" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "wasi" | name = "wasi" | ||||||
| version = "0.11.0+wasi-snapshot-preview1" | version = "0.11.0+wasi-snapshot-preview1" | ||||||
|   | |||||||
| @@ -8,9 +8,9 @@ default-run = "aoc" | |||||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||||
|  |  | ||||||
| [dependencies] | [dependencies] | ||||||
| # framework |  | ||||||
| dotenv = "0.15.0" | dotenv = "0.15.0" | ||||||
|  | hex-literal = "0.3.4" | ||||||
| itertools = "0.10.5" | itertools = "0.10.5" | ||||||
| pico-args = "0.5.0" | pico-args = "0.5.0" | ||||||
| reqwest = { version = "0.11.13", features = ["blocking"] } | reqwest = { version = "0.11.13", features = ["blocking"] } | ||||||
| # for solving | rust-crypto = "0.2.36" | ||||||
|   | |||||||
							
								
								
									
										57
									
								
								src/bin/04.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/bin/04.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | |||||||
|  | use crypto::digest::Digest; | ||||||
|  | use crypto::md5::Md5; | ||||||
|  |  | ||||||
|  | pub fn part_one(input: &str) -> Option<u32> { | ||||||
|  |     let key = input.trim().as_bytes(); | ||||||
|  |     let mut hasher = Md5::new(); | ||||||
|  |     for x in 0..std::u64::MAX { | ||||||
|  |         hasher.input(key); | ||||||
|  |         hasher.input(x.to_string().as_bytes()); | ||||||
|  |  | ||||||
|  |         let mut output = [0; 16]; | ||||||
|  |         hasher.result(&mut output); | ||||||
|  |  | ||||||
|  |         let first_five = output[0] as u32 + output[1] as u32 + (output[2] >> 4) as u32; | ||||||
|  |         if first_five == 0 { | ||||||
|  |             return Some(x as u32); | ||||||
|  |         } | ||||||
|  |         hasher.reset(); | ||||||
|  |     } | ||||||
|  |     None | ||||||
|  | } | ||||||
|  | pub fn part_two(input: &str) -> Option<u32> { | ||||||
|  |     let key = input.trim().as_bytes(); | ||||||
|  |     let mut hasher = Md5::new(); | ||||||
|  |     for x in 0..std::u64::MAX { | ||||||
|  |         hasher.input(key); | ||||||
|  |         hasher.input(x.to_string().as_bytes()); | ||||||
|  |  | ||||||
|  |         let mut output = [0; 16]; | ||||||
|  |         hasher.result(&mut output); | ||||||
|  |  | ||||||
|  |         if output.starts_with(&[0,0,0]) { return Some(x as u32)} | ||||||
|  |         hasher.reset(); | ||||||
|  |     } | ||||||
|  |     None | ||||||
|  | } | ||||||
|  | fn main() { | ||||||
|  |     let input = &aoc::read_file("inputs", 4); | ||||||
|  |     aoc::solve!(1, part_one, input); | ||||||
|  |     aoc::solve!(2, part_two, input); | ||||||
|  | } | ||||||
|  | #[cfg(test)] | ||||||
|  | mod tests { | ||||||
|  |     use super::*; | ||||||
|  |     #[test] | ||||||
|  |     fn test_part_one() { | ||||||
|  |         // let input = aoc::read_file("test_inputs", 4); | ||||||
|  |         let input = "abcdef"; | ||||||
|  |         assert_eq!(part_one(&input), Some(609043)); | ||||||
|  |     } | ||||||
|  |     #[test] | ||||||
|  |     fn test_part_two() { | ||||||
|  |         // let input = aoc::read_file("test_inputs", 4); | ||||||
|  |         // let input = "abcdef"; | ||||||
|  |         // assert_eq!(part_two(&input), None); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user