Skip to content

Commit

Permalink
letter_boxed_solver: St/Ch two_chars()
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Mar 29, 2021
1 parent 434201f commit 7c56202
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions letter_boxed_solver/src/lib.rs
Expand Up @@ -7,13 +7,17 @@ use std::{
pub enum Ch {
None,
A,
B,
C,
}

impl Display for Ch {
fn fmt(&self, f: &mut Formatter) -> Result {
let s = match self {
Ch::None => "",
Ch::A => "A",
Ch::B => "B",
Ch::C => "C",
};
write!(f, "{}", s)
}
Expand All @@ -33,7 +37,14 @@ impl St {

impl Display for St {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self[0])
for i in 0..self.len() {
let r = write!(f, "{}", self[i]);
if r.is_err() {
return r
}
}

Ok(())
}
}

Expand All @@ -44,6 +55,8 @@ impl Index<u8> for St {
let c = (self.0 >> (4 + 5 * index)) & 0x1F;
match c {
1 => &Ch::A,
2 => &Ch::B,
3 => &Ch::C,
_ => &Ch::None,
}
}
Expand All @@ -52,8 +65,14 @@ impl Index<u8> for St {
impl Add<Ch> for St {
type Output = St;

fn add(self, _rhs: Ch) -> Self::Output {
St(0x11)
fn add(self, rhs: Ch) -> Self::Output {
let c = match rhs {
Ch::None => 0,
Ch::A => 1,
Ch::B => 2,
Ch::C => 3,
};
St((self.0 + 1) | (c << (4 + 5 * self.len())))
}
}

Expand Down Expand Up @@ -108,4 +127,28 @@ mod tests {
let actual: Vec<Ch> = (0..12).map(|i| s[i]).collect();
assert_eq!(expected, actual);
}

#[test]
fn two_chars() {
let s = St::empty() + Ch::B + Ch::C;

assert_eq!(2, s.len());
assert_eq!("BC", s.to_string());
let expected = vec![
Ch::B,
Ch::C,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
Ch::None,
];
let actual: Vec<Ch> = (0..12).map(|i| s[i]).collect();
assert_eq!(expected, actual);
}
}

0 comments on commit 7c56202

Please sign in to comment.