Files
aoc2016/day05/src/main.rs
T
2026-06-27 17:59:40 +02:00

69 lines
1.7 KiB
Rust

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();
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;
print!("\r{}", result.iter().collect::<String>());
std::io::stdout().flush().unwrap();
}
}
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!();
}