This commit is contained in:
2016-12-21 14:08:42 +01:00
parent d08be91392
commit b0e30b1726
3 changed files with 222 additions and 0 deletions

81
2016/17/Cargo.lock generated Normal file
View File

@@ -0,0 +1,81 @@
[root]
name = "17"
version = "0.1.0"
dependencies = [
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rust-crypto"
version = "0.2.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "time"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum gcc 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "872db9e59486ef2b14f8e8c10e9ef02de2bccef6363d7f34835dedb386b3d950"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

7
2016/17/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "17"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]
rust-crypto = "0.2"

134
2016/17/src/main.rs Normal file
View 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']));
}
}