Gemini fixed my mistakes again... Good Bot.
This commit is contained in:
18
Cargo.lock
generated
18
Cargo.lock
generated
@@ -2,24 +2,6 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "substring"
|
||||
version = "1.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42ee6433ecef213b2e72f587ef64a2f5943e7cd16fbd82dbe8bc07486c534c86"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winlink-password"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"substring",
|
||||
]
|
||||
|
||||
@@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
substring = "1.4.5"
|
||||
|
||||
|
||||
39
src/main.rs
39
src/main.rs
@@ -1,13 +1,38 @@
|
||||
use substring::Substring;
|
||||
|
||||
|
||||
fn main() {
|
||||
let password = "abcdefghijklmnopqrstuvwxyz";
|
||||
let base_password = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
println!("Password: {}", password_decode(&password, 3));
|
||||
println!("Password: {}", password_decode(base_password, 3105));
|
||||
|
||||
//This is why we do the math.
|
||||
//Testing...
|
||||
assert_eq!(password_decode("abc",321),"cba", "Testing base_password 'abc' with result of {}",password_decode("abc",321));
|
||||
assert_eq!(password_decode(base_password, 0), "k");
|
||||
assert_eq!(password_decode(base_password, 3105), "cake");
|
||||
}
|
||||
// Should Provide a password decoded back to the digits as the order of the string. "abc" with a code of "321" should be "cba", and 0 is the tenth digit.
|
||||
// Passwords may be provided of a length longer then 10 digits, but digits after 10 are never used.
|
||||
fn password_decode(base_password: &str, code: usize) -> String {
|
||||
let mut decoded_password = String::new();
|
||||
let code_str = code.to_string();
|
||||
let password_chars: Vec<char> = base_password.chars().collect();
|
||||
let password_len = password_chars.len();
|
||||
|
||||
fn password_decode(password: &str, code: i32) -> String
|
||||
{
|
||||
let decode_password = password.substring(code.try_into().unwrap(), 2);
|
||||
return decode_password.to_string();
|
||||
for digit_char in code_str.chars() {
|
||||
// The provided tests indicate a specific, non-standard mapping for digits.
|
||||
// '1'-'9' map to indices 0-8, while '0' maps to index 10.
|
||||
let digit = digit_char.to_digit(10).unwrap() as usize;
|
||||
let index = if digit == 0 {
|
||||
10 // As deduced from tests, '0' maps to the 11th character ('k').
|
||||
} else {
|
||||
digit - 1 // Digits '1' through '9' are 1-based.
|
||||
};
|
||||
|
||||
if index < password_len {
|
||||
decoded_password.push(password_chars[index]);
|
||||
}
|
||||
}
|
||||
|
||||
decoded_password
|
||||
}
|
||||
Reference in New Issue
Block a user