2016/17
This commit is contained in:
134
2016/17/src/main.rs
Normal file
134
2016/17/src/main.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
extern crate crypto;
|
||||
|
||||
use crypto::md5::Md5;
|
||||
use crypto::digest::Digest;
|
||||
|
||||
|
||||
pub fn is_open(input: &char) -> bool {
|
||||
match *input {
|
||||
'b' ... 'f' => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate(passcode: &str, path: &Vec<char>) -> Vec<char> {
|
||||
let input = format!("{}{}", passcode, path.iter().map(|x| *x).collect::<String>());
|
||||
|
||||
let mut md5 = Md5::new();
|
||||
|
||||
md5.input(input.as_bytes());
|
||||
|
||||
let hash = md5.result_str().chars().take(4).collect::<Vec<_>>();
|
||||
|
||||
let mut directions = Vec::new();
|
||||
|
||||
if is_open(&hash[0]) {
|
||||
directions.push('U');
|
||||
}
|
||||
|
||||
if is_open(&hash[1]) {
|
||||
directions.push('D');
|
||||
}
|
||||
|
||||
if is_open(&hash[2]) {
|
||||
directions.push('L');
|
||||
}
|
||||
|
||||
if is_open(&hash[3]) {
|
||||
directions.push('R');
|
||||
}
|
||||
|
||||
directions
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let passcode = "yjjvjgan";
|
||||
|
||||
let mut variants: Vec<(Vec<char>, (usize, usize))> = Vec::new();
|
||||
|
||||
variants.push((vec![], (0, 0)));
|
||||
|
||||
while !variants.is_empty() {
|
||||
let (path, (x, y)) = variants.remove(0);
|
||||
|
||||
if x == 3 && y == 3 {
|
||||
let end_path = path.iter().map(|c| *c).collect::<String>();
|
||||
|
||||
println!("{} -> {:?}", end_path.len(), end_path);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
for m in generate(passcode, &path) {
|
||||
match m {
|
||||
'U' => {
|
||||
if y > 0 {
|
||||
let mut m_path = path.clone();
|
||||
|
||||
m_path.push(m);
|
||||
|
||||
variants.push((m_path, (x, y - 1)));
|
||||
}
|
||||
},
|
||||
'D' => {
|
||||
if y < 3 {
|
||||
let mut m_path = path.clone();
|
||||
|
||||
m_path.push(m);
|
||||
|
||||
variants.push((m_path, (x, y + 1)));
|
||||
}
|
||||
},
|
||||
'L' => {
|
||||
if x > 0 {
|
||||
let mut m_path = path.clone();
|
||||
|
||||
m_path.push(m);
|
||||
|
||||
variants.push((m_path, (x - 1, y)));
|
||||
}
|
||||
},
|
||||
'R' => {
|
||||
if x < 3 {
|
||||
let mut m_path = path.clone();
|
||||
|
||||
m_path.push(m);
|
||||
|
||||
variants.push((m_path, (x + 1, y)));
|
||||
}
|
||||
},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_is_open() {
|
||||
assert_eq!(true, is_open(&'c'));
|
||||
assert_eq!(false, is_open(&'0'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_01() {
|
||||
assert_eq!(vec!['U', 'D', 'L'], generate("hijkl", &vec![]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_02() {
|
||||
assert_eq!(vec!['U', 'L', 'R'], generate("hijkl", &vec!['D']));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_03() {
|
||||
let expected: Vec<char> = vec![];
|
||||
|
||||
assert_eq!(expected, generate("hijkl", &vec!['D', 'R']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user