DEV Community

Discussion on: Daily Challenge #227 - Android Lock Screen

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Rust, precomputed here

#[derive(Debug)]
enum BadInputError {
    ZeroLength,
    FirstPoint(char),
}
fn count_patterns_from(first_point: char, length: usize) -> Result<u16, BadInputError> {
    if length < 2 {
        return Err(BadInputError::ZeroLength);
    }
    Ok(match first_point {
        'A' | 'C' | 'G' | 'I' => [1, 5, 31, 154, 684, 2516, 7104, 13792, 13792],
        'B' | 'D' | 'F' | 'H' => [1, 7, 37, 188, 816, 2926, 8118, 15564, 15564],
        'E' => [1, 8, 48, 256, 1152, 4248, 12024, 23280, 23280],
        bad => return Err(BadInputError::FirstPoint(first_point)),
    }
    .get(length - 2)
    .cloned()
    .unwrap_or(0))
}

fn main() {
    for start in (b'A'..=b'I').map(char::from) {
        println!("starting at {}, there are", start);
        for length in 1..=10 {
            match count_patterns_from(start, length) {
                Ok(num) => println!("\t{} patterns of length {}", num, length),
                Err(err) => eprintln!("{:?}", err),
            }
        }
    }
}

Deviations from spec: function named in snake_case, takes a char and not a single-char string.
Deviations from test cases: errors on length 1, as phones don't accept that, you need at least two anchors.