1
0
mirror of https://gitlab.com/MisterBiggs/aoc_2015-rust.git synced 2025-06-16 06:56:42 +00:00

Finish day 4

This commit is contained in:
Anson 2022-11-05 16:35:12 -06:00
parent b1aaa2f7ae
commit 451a314af5
4 changed files with 51 additions and 0 deletions

9
Cargo.lock generated
View File

@ -5,3 +5,12 @@ version = 3
[[package]] [[package]]
name = "AoC2015" name = "AoC2015"
version = "0.1.0" version = "0.1.0"
dependencies = [
"md5",
]
[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"

View File

@ -6,3 +6,4 @@ edition = "2021"
# 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]
md5 = "0.7.0"

39
src/day4.rs Normal file
View File

@ -0,0 +1,39 @@
use md5;
pub fn run() {
println!("Day 4:");
let input = "yzbqklnj".to_string();
println!("\tPart 1: {}", part1(&input, 5));
println!("\tPart 2: {}", part1(&input, 6));
}
fn part1(input: &String, zeros: usize) -> usize {
let mut key: usize = 0;
let max_iter = 1e9 as usize;
loop {
let digest = md5::compute(format!("{}{}", input, key));
if format!("{:x},", digest).starts_with(&"0".repeat(zeros)) {
return key;
}
key += 1;
if key == max_iter {
return 0;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_1() {
assert_eq!(part1(&"abcdef".to_string(), 5), 609043);
assert_eq!(part1(&"pqrstuv".to_string(), 5), 1048970);
}
}

View File

@ -1,10 +1,12 @@
mod day1; mod day1;
mod day2; mod day2;
mod day3; mod day3;
mod day4;
fn main() { fn main() {
println!("Running Advent of Code 2021"); println!("Running Advent of Code 2021");
day1::run(); day1::run();
day2::run(); day2::run();
day3::run(); day3::run();
day4::run();
} }