DEV Community

Discussion on: Daily Challenge #222 - Parse Bank Account Numbers

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Rust. Who needs error checking when you can support multiline instead?

use itertools::{izip, Itertools}; // 0.9.0
fn smol(big: &str) -> String {
    big.lines()
        .tuples()
        .flat_map(|(l1, l2, l3)| {
            izip!(l1.chars(), l2.chars(), l3.chars())
                .tuples()
                .map(|digit| match digit {
                    ((' ', ' ', ' '), (' ', ' ', ' '), (' ', '|', '|')) => '1',
                    ((' ', ' ', '|'), ('_', '_', '_'), (' ', '|', ' ')) => '2',
                    ((' ', ' ', ' '), ('_', '_', '_'), (' ', '|', '|')) => '3',
                    ((' ', '|', ' '), (' ', '_', ' '), (' ', '|', '|')) => '4',
                    ((' ', '|', ' '), ('_', '_', '_'), (' ', ' ', '|')) => '5',
                    ((' ', '|', '|'), ('_', '_', '_'), (' ', ' ', '|')) => '6',
                    ((' ', ' ', ' '), ('_', ' ', ' '), (' ', '|', '|')) => '7',
                    ((' ', '|', '|'), ('_', '_', '_'), (' ', '|', '|')) => '8',
                    ((' ', '|', ' '), ('_', '_', '_'), (' ', '|', '|')) => '9',
                    x => unreachable!(),
                })
        })
        .collect()
}

fn main() {
    println!(
        "{}",
        smol(
            r"    _  _     _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|
    _  _     _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|
    _  _     _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|"
        )
    );
}

Look at it go