From 49633b8504463266127d02be49373d53315df1e1 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Fri, 6 Dec 2024 10:04:09 +0100 Subject: [PATCH] y2016d5 --- Cargo.lock | 3 ++ Cargo.toml | 1 + y2015/Cargo.toml | 2 +- y2016/Cargo.toml | 1 + y2016/src/bin/d5.rs | 14 ++++++++ y2016/src/days/d5.rs | 83 +++++++++++++++++++++++++++++++++++++++++++ y2016/src/days/mod.rs | 2 ++ 7 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 y2016/src/bin/d5.rs create mode 100644 y2016/src/days/d5.rs diff --git a/Cargo.lock b/Cargo.lock index cf62ab4..0b7002d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,9 @@ dependencies = [ [[package]] name = "y2016" version = "0.1.0" +dependencies = [ + "md5", +] [[package]] name = "y2017" diff --git a/Cargo.toml b/Cargo.toml index 5b777a3..2a740e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ members = [ utils = { path = "../utils" } itertools = "0.13.0" regex = "1.11.1" +md5 = "0.7.0" diff --git a/y2015/Cargo.toml b/y2015/Cargo.toml index 7d7cee6..11234e1 100644 --- a/y2015/Cargo.toml +++ b/y2015/Cargo.toml @@ -4,6 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -md5 = "0.7.0" +md5 = { workspace = true } utils = { workspace = true } itertools = { workspace = true } diff --git a/y2016/Cargo.toml b/y2016/Cargo.toml index 8812d24..48faf27 100644 --- a/y2016/Cargo.toml +++ b/y2016/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +md5 = { workspace = true } diff --git a/y2016/src/bin/d5.rs b/y2016/src/bin/d5.rs new file mode 100644 index 0000000..c6c926c --- /dev/null +++ b/y2016/src/bin/d5.rs @@ -0,0 +1,14 @@ +use y2016::days::d5; + +fn main() { + part1(); + part2(); +} + +fn part1() { + println!("{}", d5::process_part1("ojvtpuvg")); +} + +fn part2() { + println!("{}", d5::process_part2("ojvtpuvg")); +} diff --git a/y2016/src/days/d5.rs b/y2016/src/days/d5.rs new file mode 100644 index 0000000..9e5b3ed --- /dev/null +++ b/y2016/src/days/d5.rs @@ -0,0 +1,83 @@ +use std::io::{stdout, Write}; + +pub fn process_part1(input: &str) -> String { + let mut password = String::new(); + let mut n = 0; + let mut col = 1; + print!("________"); + loop { + let hash = md5::compute(format!("{input}{n}")); + let hash = format!("{hash:x}"); + let mut chars = hash.chars().skip(5); + let char = chars.next().unwrap(); + print!("\x1b[{col}G{char}"); + if hash.split_at(5).0 == "00000" { + col += 1; + password.push(char); + } + if password.len() == 8 { + println!(); + return password; + } + n += 1; + } +} + +pub fn process_part2(input: &str) -> String { + let mut password = vec!['_'; 8]; + let mut n = 0; + print!("________"); + loop { + if !password.contains(&'_') { + println!(); + return password.into_iter().collect(); + } + let hash = md5::compute(format!("{input}{n}")); + let hash = format!("{hash:x}"); + if hash.split_at(5).0 == "00000" { + let mut chars = hash.chars().skip(5); + let position = match chars.next().unwrap().to_digit(10) { + Some(position) => { + if position > 7 { + n += 1; + continue; + } + if password[position as usize] != '_' { + n += 1; + continue; + } + position + } + None => { + n += 1; + continue; + } + }; + let char = chars.next().unwrap(); + print!("\x1b[{}G{char}", position + 1); + //println!("{}", password.clone().into_iter().collect::()); + let _ = stdout().flush(); + password[position as usize] = char; + } + n += 1; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT: &str = "abc"; + + #[test] + fn part1() { + let result = process_part1(INPUT); + assert_eq!(result, "18f47a30".to_string()); + } + + #[test] + fn part2() { + let result = process_part2(INPUT); + assert_eq!(result, "05ace8e3".to_string()); + } +} diff --git a/y2016/src/days/mod.rs b/y2016/src/days/mod.rs index 69d8311..5b5d285 100644 --- a/y2016/src/days/mod.rs +++ b/y2016/src/days/mod.rs @@ -5,3 +5,5 @@ pub mod d2; pub mod d3; pub mod d4; + +pub mod d5;