use std::char;
fn check(vec: &Vec<u8>) -> bool {
let mut acc: u32 = 0;
for &i in vec {
// sifreyi_bul_01 is accepting
// characters between 48 and 128
if i < 48 || i > 128 {
return false;
}
let i = i - 48;
match i {
5 => acc *= i as u32,
2 => acc /= i as u32,
_ => acc += i as u32
}
}
if acc == 178 {
true
} else {
false
}
}
fn to_string(vec: &Vec<u8>) -> String {
let mut s = String::new();
for &i in vec {
match char::from_u32(i as u32) {
Some(c) => s.push(c),
_ => return "".to_string()
};
}
s
}
// test "check" and "to_string" first
// rust is making tdd really easy
#[test]
fn test_check() {
assert_eq!(check(&vec!(126, 126, 70)), true);
assert_eq!(check(&vec!(126, 126, 69, 49)), true);
assert_eq!(check(&vec!(126, 126, 68, 49, 49)), true);
assert_eq!(check(&vec!(83, 53, 51)), true);
// 128 should pass
assert_eq!(check(&vec!(0x7f, 0x7f, 0x44)), true);
assert_eq!(check(&vec!(0)), false);
assert_eq!(check(&vec!(0, 0, 0)), false);
assert_eq!(check(&vec!(48, 48, 48)), false);
}
#[test]
fn test_to_string() {
assert_eq!(to_string(&vec!(126, 126, 70)), "~~F");
assert_eq!(to_string(&vec!(83, 53, 51)), "S53");
// this kind of string should never generated
// but still its good to test it
assert_eq!(to_string(&vec!(0, 0, 0)), "\u{0}\u{0}\u{0}");
assert_eq!(to_string(&vec!(0xff, 0xff, 0xff)), "\u{ff}\u{ff}\u{ff}");
}
fn find_right_vecs(vec: &mut Vec<u8>, n: usize, depth: usize) {
// check only readable characters
for x in 49..127 {
vec[n] = x as u8;
if n < depth {
find_right_vecs(vec, n+1, depth);
}
if check(&vec) {
println!("{}", to_string(&vec));
}
}
}
fn find_vecs(max: usize) {
for i in 0..max {
let mut vec: Vec<u8> = vec![0; i + 1];
find_right_vecs(&mut vec, 0, i);
}
}
fn main() {
// find stuff for only maximum 3 characters
find_vecs(3);
}
CnVzZSBzdGQ6OmNoYXI7CgoKZm4gY2hlY2sodmVjOiAmVmVjPHU4PikgLT4gYm9vbCB7CiAgIC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