y2016d5
This commit is contained in:
parent
8fd9bcd63a
commit
49633b8504
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -87,6 +87,9 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "y2016"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"md5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "y2017"
|
||||
|
@ -22,3 +22,4 @@ members = [
|
||||
utils = { path = "../utils" }
|
||||
itertools = "0.13.0"
|
||||
regex = "1.11.1"
|
||||
md5 = "0.7.0"
|
||||
|
@ -4,6 +4,6 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
md5 = "0.7.0"
|
||||
md5 = { workspace = true }
|
||||
utils = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
md5 = { workspace = true }
|
||||
|
14
y2016/src/bin/d5.rs
Normal file
14
y2016/src/bin/d5.rs
Normal file
@ -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"));
|
||||
}
|
83
y2016/src/days/d5.rs
Normal file
83
y2016/src/days/d5.rs
Normal file
@ -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::<String>());
|
||||
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());
|
||||
}
|
||||
}
|
@ -5,3 +5,5 @@ pub mod d2;
|
||||
pub mod d3;
|
||||
|
||||
pub mod d4;
|
||||
|
||||
pub mod d5;
|
||||
|
Loading…
Reference in New Issue
Block a user