less overengineering

This commit is contained in:
Thomas Lindner 2016-09-09 19:26:03 +02:00
parent 1bb7462072
commit a5467c7293

View file

@ -6,35 +6,6 @@ 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);
@ -55,13 +26,16 @@ fn main() {
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)
match (line1.from_hex(), line2.from_hex()) {
(Ok(v1), Ok(v2)) => {
let mut out = Vec::with_capacity(min(v1.len(), v2.len()));
for (lhs, rhs) in v1.iter().zip(v2) {
out.push(lhs ^ rhs)
}
println!("{}", out.to_hex())
},
Err(e) => error!("{}", e)
(Err(e), _) => error!("{}", e),
(_, Err(e)) => error!("{}", e)
}
}
}