solve Challenge 2 of Set 1

master
Thomas Lindner 2016-09-09 19:08:55 +02:00
parent 150c9e067a
commit 1bb7462072
3 changed files with 183 additions and 0 deletions

107
set1/fixed_xor/Cargo.lock generated Normal file
View File

@ -0,0 +1,107 @@
[root]
name = "fixed_xor"
version = "0.1.0"
dependencies = [
"env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.75 (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.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.1.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustc-serialize"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread-id"
version = "2.0.0"
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.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "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"

View File

@ -0,0 +1,9 @@
[package]
name = "fixed_xor"
version = "0.1.0"
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
[dependencies]
log = "0.3.6"
env_logger = "0.3.4"
rustc-serialize = "0.3"

View File

@ -0,0 +1,67 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
use rustc_serialize::hex::{FromHex, ToHex};
use std::cmp::min;
use std::io::{BufRead, stdin};
use std::ops::BitXor;
struct BitVec(Vec<u8>);
impl From<BitVec> for Vec<u8> {
fn from(bv: BitVec) -> Self {
bv.0
}
}
impl From<Vec<u8>> for BitVec {
fn from(v: Vec<u8>) -> Self {
BitVec(v)
}
}
impl BitXor for BitVec {
type Output = BitVec;
fn bitxor(self, rhs: BitVec) -> BitVec {
let lhs: Vec<u8> = self.into();
let rhs: Vec<u8> = rhs.into();
let mut out = Vec::with_capacity(min(lhs.len(), rhs.len()));
for (lhs, rhs) in lhs.iter().zip(rhs) {
out.push(lhs ^ rhs)
}
BitVec(out)
}
}
struct Pairwise<T: Iterator>(T);
impl<T> Iterator for Pairwise<T> where T: Iterator {
type Item = (T::Item, T::Item);
fn next(&mut self) -> Option<Self::Item> {
if let (Some(fst), Some(snd)) = (self.0.next(), self.0.next()) {
Some((fst, snd))
} else {
None
}
}
}
fn main() {
env_logger::init().unwrap();
let stdin = stdin();
for (line1, line2) in Pairwise(stdin.lock().lines().filter_map(|x| x.ok())) {
match line1.from_hex() {
Ok(v1) =>
match line2.from_hex() {
Ok(v2) => println!("{}", <Vec<u8>>::from(<BitVec>::from(v1) ^ <BitVec>::from(v2)).to_hex()),
Err(e) => error!("{}", e)
},
Err(e) => error!("{}", e)
}
}
}