/
binary.rs
76 lines (63 loc) · 1.89 KB
/
binary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::env;
use thiserror::Error;
#[derive(Error, Debug)]
enum Error {
#[error("invalid input character: '{0}'")]
InvalidCharacter(char),
#[error("mismatched input sizes")]
MismatchedInputSizes,
#[error("insufficient CLI arguments")]
InsufficientCliArguments,
}
fn read_binary(b_str: &str) -> Result<Vec<bool>, Error> {
let mut res = Vec::new();
for ch in b_str.chars().rev() {
res.push(match ch {
'0' => false,
'1' => true,
_ => {
return Err(Error::InvalidCharacter(ch));
}
});
}
Ok(res)
}
fn write_binary(b_bits: &[bool]) -> String {
let mut res = "".to_string();
for b in b_bits.iter().rev() {
res.push(if *b { '1' } else { '0' });
}
res
}
fn add(a: &[bool], b: &[bool]) -> Result<Vec<bool>, Error> {
let mut carry = false;
let mut res = Vec::new();
if a.len() != b.len() {
return Err(Error::MismatchedInputSizes);
}
for i in 0..a.len() {
let (output, new_carry) = match (a[i], b[i], carry) {
(false, false, false) => (false, false),
(false, false, true) => (true, false),
(false, true, false) => (true, false),
(false, true, true) => (false, true),
(true, false, false) => (true, false),
(true, false, true) => (false, true),
(true, true, false) => (false, true),
(true, true, true) => (true, true),
};
carry = new_carry;
res.push(output);
}
Ok(res)
}
fn main() -> Result<(), Error> {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
eprintln!("Provide two arguments of equal length");
return Err(Error::InsufficientCliArguments);
}
let result = add(&read_binary(&args[1])?, &read_binary(&args[2])?)?;
println!("{}", write_binary(&result));
Ok(())
}