add day 5

This commit is contained in:
timeshifter
2026-06-27 14:47:05 +02:00
parent fd7d6f582a
commit 32f6018b93
3 changed files with 92 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
use std::io::Write;
#[cfg(debug_assertions)]
const INPUT: &str = "abc";
#[cfg(not(debug_assertions))]
const INPUT: &str = "cxdnnyjw";
// const INPUT: &str = "abc";
fn main() {
part1();
println!();
part2();
}
fn part2() {
let mut result = [' '; 8];
for i in 0_i64.. {
let s = format!("{INPUT}{i}");
let digest = md5::compute(s);
let result_string = format!("{:x}", digest);
if result_string.chars().take(5).all(|c| c == '0') {
let mut chars = result_string.chars();
let the_position = chars.nth(5).unwrap();
let the_char = chars.next().unwrap();
if ('0'..'8').contains(&the_position) {
let position = the_position.to_string().parse::<usize>().unwrap();
if result[position] == ' ' {
result[position] = the_char;
println!("{}", result.iter().collect::<String>());
}
}
if !result.contains(&' ') {
break;
}
};
}
println!();
}
fn part1() {
let mut result = vec![];
for i in 0_i64.. {
let s = format!("{INPUT}{i}");
let digest = md5::compute(s);
let result_string = format!("{:x}", digest);
if let Some(the_char) = result_string
.chars()
.take(5)
.all(|c| c == '0')
.then(|| result_string.chars().nth(5).unwrap())
{
print!("{the_char}");
std::io::stdout().flush().unwrap();
result.push(the_char);
if result.len() == 8 {
break;
}
};
}
println!();
}